I have a table with some data in it:
ColA | ColB | ColC ------+------+------ 1 | A | X 2 | A | Y 3 | B | Y 4 | C | Y 5 | C | Z 6 | D | Y 7 | D | Z
I want to query to get all of the rows where ColB
and ColC
as a pair match a condition:
SELECT * FROM [Table]
WHERE (ColB = A AND ColC = Y)
OR (ColB = B AND ColC = Y)
OR (ColB = C AND ColC = Y)
OR (ColB = D AND ColC = Z)
this should return rows 2, 3, 4 and 7.
The pairs of values for (ColB, ColC) could potentially be large (in the region of ~100). Is there a more efficient way of querying for this data other than a large query with lots of OR
conditions?
I am hoping there is a way of using an equivalent of a Tuple, meaning I can do something like:
SELECT * FROM [Table]
WHERE (ColB, ColC) IN ({A, Y}, {B, Y}, {C, Y}, {D, Z})
Any ideas?
EDIT: (to answer some questions in the comments)
The fields for ColB
and ColC
store guids and are declared as uniqueidentifier
types.
This needs to work on SQL Server 2005 upwards (all editions).
The table has in the order of millions of rows, and I'm not averse to adding any indices that are required for this to work.