How I can select a set of rows where each row match a different condition?
Example:
Supposing I have a table with a column called name, I want the result ONLY IF the first row name matches ‘A’, the second row name matches ‘B’ and the third row name matches ‘C’.
Edit:
I want to do this to work without a fixed size, but in a way I can define the sequence like R,X,V,P,T and it matches the sequence, each one in a row, but in the order.
Assuming that You know how to provide a row number to your rows (ROW_NUMBER() in SQL Server, for instance), You can create a lookup (match) table and join on it. See below for explanation:
LookupTable:
Your SourceTable source table (assuming You already added RowNum to it-in case You didn’t, just introduce subquery for it (or CTE for SQL Server 2005 or newer):
Now You need to inner join LookupTable with your SourceTable on
LookupTable.RowNum = SourceTable.RowNum AND LookupTable.Name = SourceTable.Name. Then do a left join of this result with LookupTable on RowNum only. If there isLookupTable.RowNum IS NULLin final result then You know that there is no complete match on at least one row.Here is code for joins:
Result set of above query will contain rows with
Matched IS NULLif row is not matching condition from LookupTable table.