Giovanni Zambotti
Giovanni Zambotti

Reputation: 79

sql query records pair of two

I have a table like this one below where the records are basically pair of two (same ID1 and ID2) but different Note and Status (some times). I need to select all the records that at the same time have the field Note equal to "Ready to QC" and the field Status equal to 1, and the records with the field Note equal to "Ready for Cataloging" have Status 0.

ID1,   ID2,      Note,                      Status
3,     22,       Ready for QC,                1
3,     22,       Ready for Cataloging,        0
36,    22,       Ready for QC,                1
36,    22,       Ready for Cataloging,        1
63,    22,       Ready for QC,                1
63,    22,       Ready for Cataloging,        0
67,    67,       Ready for QC,                0
67,    67,       Ready for Cataloging,        0

Any suggestions? Thank you. Giovanni

Upvotes: 3

Views: 94

Answers (2)

kjmerf
kjmerf

Reputation: 4345

This one is longer but accounts for duplicates:

SELECT t1.id1, t1.id2, t1.note, t1.status
FROM
(SELECT *
 FROM t
 WHERE note = 'Ready for QC'
 AND status = 1) t1
INNER JOIN
(SELECT *
 FROM t
 WHERE note = 'Ready for Cataloging'
 AND status = 0) t2
ON t1.id1 = t2.id1
AND t1.id2 = t2.id2

Upvotes: 0

xQbert
xQbert

Reputation: 35333

I think you want all the ID1 and ID2 of such records where both conditions exist..

It does assume a uniqueness for each ID1, ID2, Note, Status (meaning there can't be two identical records in the table)

SELECT ID1, ID2, count(*) cnt
FROM Table
WHERE (Note = "Ready to QC" and status = 1) 
    OR(note = "Ready for Cataloging" and Status 0)
GROUP BY ID1, ID2
HAVING COUNT(*) = 2

Upvotes: 4

Related Questions