Reputation: 928
Let's say I have following table:
ID | condition1 | condition2
1 | Y | 0
2 | N | 0
3 | Y | 1
4 | N | 1
I am searching for a query that gives me the results where condition1
equals Y. But, if it doesn't equal Y, I want to check condition2
and add those to my result where condition2
equals 1.
So, my result would be:
ID | condition1 | condition2
1 | Y | 0
3 | Y | 1
4 | N | 1
Any suggestions/ideas?
EDIT: I just realized my problem is not as simple as the example I wanted to give. (Totally my own fault). Since there are multiple answers who do give a solution to this (wrong) question, I won't edit it but just leave it like it is in case someone needs it in the future.
Upvotes: 0
Views: 38
Reputation: 3515
Try this:
SELECT ID, condtition1, condition2
FROM YourTable
WHERE condition1 = 'Y'
OR (condition1 != 'Y' AND condition2 = 0);
Upvotes: 1
Reputation: 5893
CREATE TABLE #B
([ID] INT, [CONDITION1] VARCHAR(1), [CONDITION2] INT)
;
INSERT INTO #B
([ID], [CONDITION1], [CONDITION2])
VALUES
(1, 'Y', 0),
(2, 'N', 0),
(3, 'Y', 1),
(4, 'N', 1)
SELECT [ID], [CONDITION1], [CONDITION2] FROM #B WHERE [CONDITION1]='Y' OR [CONDITION2]=1
output
ID condition1 condition2
1 Y 0
3 Y 1
4 N 1
Upvotes: 4