tags:

views:

191

answers:

3

I have a query where I join a table on itself to find mismatches between a parts height or width. The only problem is that because of this join it will return each miss-match twice for each part. I only want to return one row for each miss-match, not two.

Here's the table:

tblTagGlass
JobID varchar
UnitCode varchar
PartCode varchar
PartQty int
TagHeight float
TagWidth float

and the query:

select *
from tblTagGlass ttg
inner join tblTagGlass ttgC ON
ttg.JobID = ttgC.JobID
AND ttg.PartCode = ttgC.PartCode
where ttg.TagHeight != ttgC.TagHeight
or ttg.TagWidth != ttgC.TagWidth
order by ttg.PartCode

and the results:

INC375 U2-052 VT2-011 1 2013   1444.5   INC375 U2-028 VT2-011 1 2012.5 1444.5
INC375 U2-028 VT2-011 1 2012.5 1444.5   INC375 U2-052 VT2-011 1 2013   1444.5

I hope this makes sense...

+1  A: 

Try

select *
from tblTagGlass ttg
inner join tblTagGlass ttgC ON
ttg.JobID = ttgC.JobID
AND ttg.PartCode = ttgC.PartCode
where (ttg.TagHeight != ttgC.TagHeight OR ttg.TagWidth != ttgC.TagWidth)
AND ((ttg.TagHeight >= ttgC.TagHeight AND ttg.TagWidth >= ttgC.TagWidth) 
    OR (ttg.TagHeight > ttgC.TagHeight AND ttg.TagWidth < ttgC.TagWidth))
order by ttg.PartCode

The difference is using > to compare them instead of !=

Tetraneutron
Thanks, this works really well!
Noah
Works well in that case, yes. However, in other cases, it won't - such as if the heights aren't equal. So the quick solution is to make that a ">" also. But then, if height is greater on one row, but width is lesser - you'll be right back where you started. See my answer for a solution.
TheSoftwareJedi
As far as I understand the problem it will still work,an example (In SQL server syntax)Both Keyvals 4 and 5 are returned which have both valA and valB different
Tetraneutron
create table Test(PK int identity(1,1), KeyVal int, valA int, valB int) insert into Test values (1, 1, 1) insert into Test values (1, 1, 1) insert into Test values (2, 1, 1) insert into Test values (2, 2, 1) insert into Test values (3, 1, 2) insert into Test values (3, 1, 1) insert into Test values (4, 1, 2) insert into Test values (4, 2, 1) insert into Test values (5, 2, 1) insert into Test values (5, 1, 2) select * from Test A join Test B on A.valA <> B.valA and A.valB > B.valB and A.KeyVal = B.KeyVal
Tetraneutron
@Tetraneutron - he's looking for the part which has width OR height different. your SQL has an AND in the join. Change that to an OR.
TheSoftwareJedi
I think its right, The check to see if they are different has an OR in it, then to restrict it to one row per difference it checks that the first table is >= the second for both columns (which by the previous check at least one column will be) - Of course as stated below if you have some key field (say a candidate key (unique) that is comparable with > you could use that (as the only greater than comparison (in which case you wouldn't use >=))
Tetraneutron
A: 

The question becomes, which information do you want from the rows. They are different rows.

If you just want the part code, this will work:

select ttg.PartCode
from tblTagGlass ttg
inner join tblTagGlass ttgC ON
ttg.JobID = ttgC.JobID
AND ttg.PartCode = ttgC.PartCode
where ttg.TagHeight != ttgC.TagHeight
or ttg.TagWidth != ttgC.TagWidth
group by ttg.PartCode
order by ttg.PartCode

Aside from that, you'll need to decide what aggregation of information you want across the rows.

TheSoftwareJedi
I want the UnitCode, PartCode, Height, and Width the mismatches
Noah
if they are mismatches, which Height/Width do you want? Your logic is a bit flawed...
TheSoftwareJedi
love getting down voted for providing the correct answer. God bless SO. lol
TheSoftwareJedi
+1  A: 

Let's make an assumption:

  • PRIMARY KEY(JobCode, UnitCode, PartCode)

What you are looking for is items with the same JobCode and PartCode but with different UnitCode values - and with a difference in either the TagHeight or TagWidth (or both). So, use the '>' trick on UnitCode to distinguish between rows and prevent duplicates, but '!=' to detect the differences in TagHeight or TagWidth:

SELECT *
    FROM tblTagGlass ttg JOIN tblTagGlass ttgC
         ON  ttg.JobID = ttgC.JobID
         AND ttg.PartCode = ttgC.PartCode
         AND ttg.UnitCode > ttgC.UnitCode
    WHERE (ttg.TagHeight != ttgC.TagHeight
       OR ttg.TagWidth != ttgC.TagWidth)
    ORDER BY ttg.PartCode
Jonathan Leffler