Executing IF statement in Where clause SQL Server
I'm looking to only return rows where if my first condition doesn't exist
then return new condition from same table, and additional conditions:
basically...:
SELECT *
FROM Table1
WHERE
Condition#1: Column1=Column2
Condition#2: AND IF Column1 = Column2 doesn't exist (matching values for same ID)
THEN return results where Column3 = Column4

Thanks.
sql
|
show 7 more comments
I'm looking to only return rows where if my first condition doesn't exist
then return new condition from same table, and additional conditions:
basically...:
SELECT *
FROM Table1
WHERE
Condition#1: Column1=Column2
Condition#2: AND IF Column1 = Column2 doesn't exist (matching values for same ID)
THEN return results where Column3 = Column4

Thanks.
sql
what you mean column1 = column2 doesnt exist?
– Juan Carlos Oropeza
Nov 14 '18 at 21:38
2
what's wrong withWHERE Column1=Column2 OR Column3 = Column4?
– scsimon
Nov 14 '18 at 21:39
2
Show us db schema, sample data, current and expected output. Please read How-to-Ask And here is a great place to START to learn how improve your question quality and get better answers. How to create a Minimal, Complete, and Verifiable example
– Juan Carlos Oropeza
Nov 14 '18 at 21:39
@scsimon My guess is if doesnt exist any rows with col1 = col2 then return rows where col3 = col4 but not sure
– Juan Carlos Oropeza
Nov 14 '18 at 21:39
1
@Johnsonium Your answer doesnt cover the first case. becauseA <> Band100 = 100and the row shouldnt be on the result.
– Juan Carlos Oropeza
Nov 14 '18 at 22:44
|
show 7 more comments
I'm looking to only return rows where if my first condition doesn't exist
then return new condition from same table, and additional conditions:
basically...:
SELECT *
FROM Table1
WHERE
Condition#1: Column1=Column2
Condition#2: AND IF Column1 = Column2 doesn't exist (matching values for same ID)
THEN return results where Column3 = Column4

Thanks.
sql
I'm looking to only return rows where if my first condition doesn't exist
then return new condition from same table, and additional conditions:
basically...:
SELECT *
FROM Table1
WHERE
Condition#1: Column1=Column2
Condition#2: AND IF Column1 = Column2 doesn't exist (matching values for same ID)
THEN return results where Column3 = Column4

Thanks.
sql
sql
edited Nov 14 '18 at 22:30
Juan Carlos Oropeza
36.8k63978
36.8k63978
asked Nov 14 '18 at 21:36
SamSam
64
64
what you mean column1 = column2 doesnt exist?
– Juan Carlos Oropeza
Nov 14 '18 at 21:38
2
what's wrong withWHERE Column1=Column2 OR Column3 = Column4?
– scsimon
Nov 14 '18 at 21:39
2
Show us db schema, sample data, current and expected output. Please read How-to-Ask And here is a great place to START to learn how improve your question quality and get better answers. How to create a Minimal, Complete, and Verifiable example
– Juan Carlos Oropeza
Nov 14 '18 at 21:39
@scsimon My guess is if doesnt exist any rows with col1 = col2 then return rows where col3 = col4 but not sure
– Juan Carlos Oropeza
Nov 14 '18 at 21:39
1
@Johnsonium Your answer doesnt cover the first case. becauseA <> Band100 = 100and the row shouldnt be on the result.
– Juan Carlos Oropeza
Nov 14 '18 at 22:44
|
show 7 more comments
what you mean column1 = column2 doesnt exist?
– Juan Carlos Oropeza
Nov 14 '18 at 21:38
2
what's wrong withWHERE Column1=Column2 OR Column3 = Column4?
– scsimon
Nov 14 '18 at 21:39
2
Show us db schema, sample data, current and expected output. Please read How-to-Ask And here is a great place to START to learn how improve your question quality and get better answers. How to create a Minimal, Complete, and Verifiable example
– Juan Carlos Oropeza
Nov 14 '18 at 21:39
@scsimon My guess is if doesnt exist any rows with col1 = col2 then return rows where col3 = col4 but not sure
– Juan Carlos Oropeza
Nov 14 '18 at 21:39
1
@Johnsonium Your answer doesnt cover the first case. becauseA <> Band100 = 100and the row shouldnt be on the result.
– Juan Carlos Oropeza
Nov 14 '18 at 22:44
what you mean column1 = column2 doesnt exist?
– Juan Carlos Oropeza
Nov 14 '18 at 21:38
what you mean column1 = column2 doesnt exist?
– Juan Carlos Oropeza
Nov 14 '18 at 21:38
2
2
what's wrong with
WHERE Column1=Column2 OR Column3 = Column4?– scsimon
Nov 14 '18 at 21:39
what's wrong with
WHERE Column1=Column2 OR Column3 = Column4?– scsimon
Nov 14 '18 at 21:39
2
2
Show us db schema, sample data, current and expected output. Please read How-to-Ask And here is a great place to START to learn how improve your question quality and get better answers. How to create a Minimal, Complete, and Verifiable example
– Juan Carlos Oropeza
Nov 14 '18 at 21:39
Show us db schema, sample data, current and expected output. Please read How-to-Ask And here is a great place to START to learn how improve your question quality and get better answers. How to create a Minimal, Complete, and Verifiable example
– Juan Carlos Oropeza
Nov 14 '18 at 21:39
@scsimon My guess is if doesnt exist any rows with col1 = col2 then return rows where col3 = col4 but not sure
– Juan Carlos Oropeza
Nov 14 '18 at 21:39
@scsimon My guess is if doesnt exist any rows with col1 = col2 then return rows where col3 = col4 but not sure
– Juan Carlos Oropeza
Nov 14 '18 at 21:39
1
1
@Johnsonium Your answer doesnt cover the first case. because
A <> B and 100 = 100 and the row shouldnt be on the result.– Juan Carlos Oropeza
Nov 14 '18 at 22:44
@Johnsonium Your answer doesnt cover the first case. because
A <> B and 100 = 100 and the row shouldnt be on the result.– Juan Carlos Oropeza
Nov 14 '18 at 22:44
|
show 7 more comments
5 Answers
5
active
oldest
votes
First you need to check what case you are. So you count how many C1 = C2 you have in your table
SELECT [id], COUNT(CASE WHEN C1 = C2 THEN 1 END) as c1_c2
FROM Table1
GROUP BY [id]
Then you have two conditions
first when you have some c1 = c2 you do
(c1 = c2 and c1_c2 > 0 )
and the second when dont have any c1 = c2
(c1_c2 = 0 and c3 = c4)
SQL DEMO:: Because is an OR and the c1_c2 count decide which side of the where is going to work.
WITH c12 as (
SELECT [id], COUNT(CASE WHEN C1 = C2 THEN 1 END) as c1_c2_cnt
FROM Table1
GROUP BY [id]
)
SELECT *
FROM Table1 T
JOIN c12 C
ON T.[id] = C.[id]
WHERE ( C.c1_c2_cnt > 0 and T.c1 = T.c2 )
OR ( C.c1_c2_cnt = 0 and T.c3 = T.c4 )
OUTPUT

add a comment |
I suspect that this will work for what the OP is after, however, the logic seems odd...
SELECT Columns
FROM YourTable YT
WHERE YT.Column1 = YT.Column2
OR (YT.Column3 = YT.Column4
AND NOT EXISTS(SELECT 1
FROM YourTable e
WHERE YT.id = e.id
AND e.Column1 = e.Column2));
Not sure how good the performance will be here though, as you may well end up with a table scan with that NOT EXISTS.
add a comment |
Well, based off the comments, here is one way.
SELECT *
FROM Table1
WHERE Column1 = Column2
UNION
SELECT *
FROM Table2
WHERE Column1 != Column2 and Column3 = Column4
add a comment |
You can do this by using simple booleans:
WHERE
Column1 = Column2
OR
Column3 = Column4
The Venn diagram for this:

All of the shaded area will be returned, including when Column1 = Column2 & Column3 = Column4 (the dark shaded intersection)
If you don't want rows where both conditions are met then you can modify the logic:
WHERE
Column1 = Column2
OR
(Column1 <> Column2
AND
Column3 = Column4)
The Venn would look the same except the intersection would be white and excluded.
colum3 = colum4 only if doesnt exist any match for column1 = column2
– Juan Carlos Oropeza
Nov 14 '18 at 22:11
Yes. This covers it. If the first condition returns false then the second condition will be evaluated. If that is true then the whole thing returns true and the row is returned. The only rows excluded are ones where neither condition is true.
– Johnsonium
Nov 14 '18 at 22:14
doesnt work like that. the first condition has to return false for all rows before you can consider the second codition
– Juan Carlos Oropeza
Nov 14 '18 at 22:19
That isn't clear above. And even if it is you can simply modify the logic slightly: Column1 = Column2 OR (Column1 <> Column2 AND Column3 = Column4). The Venn would then have the intersection white and excluded.
– Johnsonium
Nov 14 '18 at 22:20
again, you need consider second condition if first condition fail for all rows. check my answer. I first calculate the count to see what case I have
– Juan Carlos Oropeza
Nov 14 '18 at 22:27
|
show 1 more comment
Another approach :
DECLARE
@t TABLE( ID INT, col1 CHAR(1), col2 CHAR(1), col3 INT, col4 INT)
INSERT INTO @t VALUES
(1234,'A','B',100,100),
(1234,'A','A',100,100),
(2468,'A','C',125,125),
(2468,'A','B',100,150)
SELECT
ID
, col1
, col2
, col3
, col4
FROM (
SELECT *, ROW_NUMBER() OVER(PARTITION BY ID, col1 ORDER BY col2) RN
FROM (
SELECT *
FROM @t
WHERE
col3 = col4
AND col1 = col2
UNION ALL
SELECT *
FROM @t
WHERE
col3 = col4
AND ID NOT IN(
SELECT ID
FROM @t
WHERE
col3 = col4
AND col1 = col2
)
) D
) E
WHERE
RN = 1
check this example rextester.com/HAMK64548 just change(A,A)for(C,C)and you wont get the desire output
– Juan Carlos Oropeza
Nov 15 '18 at 14:52
@JuanCarlosOropeza, I guess this will work as expected.
– iSR5
Nov 15 '18 at 16:42
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53309088%2fexecuting-if-statement-in-where-clause-sql-server%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
First you need to check what case you are. So you count how many C1 = C2 you have in your table
SELECT [id], COUNT(CASE WHEN C1 = C2 THEN 1 END) as c1_c2
FROM Table1
GROUP BY [id]
Then you have two conditions
first when you have some c1 = c2 you do
(c1 = c2 and c1_c2 > 0 )
and the second when dont have any c1 = c2
(c1_c2 = 0 and c3 = c4)
SQL DEMO:: Because is an OR and the c1_c2 count decide which side of the where is going to work.
WITH c12 as (
SELECT [id], COUNT(CASE WHEN C1 = C2 THEN 1 END) as c1_c2_cnt
FROM Table1
GROUP BY [id]
)
SELECT *
FROM Table1 T
JOIN c12 C
ON T.[id] = C.[id]
WHERE ( C.c1_c2_cnt > 0 and T.c1 = T.c2 )
OR ( C.c1_c2_cnt = 0 and T.c3 = T.c4 )
OUTPUT

add a comment |
First you need to check what case you are. So you count how many C1 = C2 you have in your table
SELECT [id], COUNT(CASE WHEN C1 = C2 THEN 1 END) as c1_c2
FROM Table1
GROUP BY [id]
Then you have two conditions
first when you have some c1 = c2 you do
(c1 = c2 and c1_c2 > 0 )
and the second when dont have any c1 = c2
(c1_c2 = 0 and c3 = c4)
SQL DEMO:: Because is an OR and the c1_c2 count decide which side of the where is going to work.
WITH c12 as (
SELECT [id], COUNT(CASE WHEN C1 = C2 THEN 1 END) as c1_c2_cnt
FROM Table1
GROUP BY [id]
)
SELECT *
FROM Table1 T
JOIN c12 C
ON T.[id] = C.[id]
WHERE ( C.c1_c2_cnt > 0 and T.c1 = T.c2 )
OR ( C.c1_c2_cnt = 0 and T.c3 = T.c4 )
OUTPUT

add a comment |
First you need to check what case you are. So you count how many C1 = C2 you have in your table
SELECT [id], COUNT(CASE WHEN C1 = C2 THEN 1 END) as c1_c2
FROM Table1
GROUP BY [id]
Then you have two conditions
first when you have some c1 = c2 you do
(c1 = c2 and c1_c2 > 0 )
and the second when dont have any c1 = c2
(c1_c2 = 0 and c3 = c4)
SQL DEMO:: Because is an OR and the c1_c2 count decide which side of the where is going to work.
WITH c12 as (
SELECT [id], COUNT(CASE WHEN C1 = C2 THEN 1 END) as c1_c2_cnt
FROM Table1
GROUP BY [id]
)
SELECT *
FROM Table1 T
JOIN c12 C
ON T.[id] = C.[id]
WHERE ( C.c1_c2_cnt > 0 and T.c1 = T.c2 )
OR ( C.c1_c2_cnt = 0 and T.c3 = T.c4 )
OUTPUT

First you need to check what case you are. So you count how many C1 = C2 you have in your table
SELECT [id], COUNT(CASE WHEN C1 = C2 THEN 1 END) as c1_c2
FROM Table1
GROUP BY [id]
Then you have two conditions
first when you have some c1 = c2 you do
(c1 = c2 and c1_c2 > 0 )
and the second when dont have any c1 = c2
(c1_c2 = 0 and c3 = c4)
SQL DEMO:: Because is an OR and the c1_c2 count decide which side of the where is going to work.
WITH c12 as (
SELECT [id], COUNT(CASE WHEN C1 = C2 THEN 1 END) as c1_c2_cnt
FROM Table1
GROUP BY [id]
)
SELECT *
FROM Table1 T
JOIN c12 C
ON T.[id] = C.[id]
WHERE ( C.c1_c2_cnt > 0 and T.c1 = T.c2 )
OR ( C.c1_c2_cnt = 0 and T.c3 = T.c4 )
OUTPUT

edited Nov 14 '18 at 22:39
answered Nov 14 '18 at 22:23
Juan Carlos OropezaJuan Carlos Oropeza
36.8k63978
36.8k63978
add a comment |
add a comment |
I suspect that this will work for what the OP is after, however, the logic seems odd...
SELECT Columns
FROM YourTable YT
WHERE YT.Column1 = YT.Column2
OR (YT.Column3 = YT.Column4
AND NOT EXISTS(SELECT 1
FROM YourTable e
WHERE YT.id = e.id
AND e.Column1 = e.Column2));
Not sure how good the performance will be here though, as you may well end up with a table scan with that NOT EXISTS.
add a comment |
I suspect that this will work for what the OP is after, however, the logic seems odd...
SELECT Columns
FROM YourTable YT
WHERE YT.Column1 = YT.Column2
OR (YT.Column3 = YT.Column4
AND NOT EXISTS(SELECT 1
FROM YourTable e
WHERE YT.id = e.id
AND e.Column1 = e.Column2));
Not sure how good the performance will be here though, as you may well end up with a table scan with that NOT EXISTS.
add a comment |
I suspect that this will work for what the OP is after, however, the logic seems odd...
SELECT Columns
FROM YourTable YT
WHERE YT.Column1 = YT.Column2
OR (YT.Column3 = YT.Column4
AND NOT EXISTS(SELECT 1
FROM YourTable e
WHERE YT.id = e.id
AND e.Column1 = e.Column2));
Not sure how good the performance will be here though, as you may well end up with a table scan with that NOT EXISTS.
I suspect that this will work for what the OP is after, however, the logic seems odd...
SELECT Columns
FROM YourTable YT
WHERE YT.Column1 = YT.Column2
OR (YT.Column3 = YT.Column4
AND NOT EXISTS(SELECT 1
FROM YourTable e
WHERE YT.id = e.id
AND e.Column1 = e.Column2));
Not sure how good the performance will be here though, as you may well end up with a table scan with that NOT EXISTS.
answered Nov 14 '18 at 21:54
LarnuLarnu
21.9k51833
21.9k51833
add a comment |
add a comment |
Well, based off the comments, here is one way.
SELECT *
FROM Table1
WHERE Column1 = Column2
UNION
SELECT *
FROM Table2
WHERE Column1 != Column2 and Column3 = Column4
add a comment |
Well, based off the comments, here is one way.
SELECT *
FROM Table1
WHERE Column1 = Column2
UNION
SELECT *
FROM Table2
WHERE Column1 != Column2 and Column3 = Column4
add a comment |
Well, based off the comments, here is one way.
SELECT *
FROM Table1
WHERE Column1 = Column2
UNION
SELECT *
FROM Table2
WHERE Column1 != Column2 and Column3 = Column4
Well, based off the comments, here is one way.
SELECT *
FROM Table1
WHERE Column1 = Column2
UNION
SELECT *
FROM Table2
WHERE Column1 != Column2 and Column3 = Column4
answered Nov 14 '18 at 21:54
scsimonscsimon
22.3k51637
22.3k51637
add a comment |
add a comment |
You can do this by using simple booleans:
WHERE
Column1 = Column2
OR
Column3 = Column4
The Venn diagram for this:

All of the shaded area will be returned, including when Column1 = Column2 & Column3 = Column4 (the dark shaded intersection)
If you don't want rows where both conditions are met then you can modify the logic:
WHERE
Column1 = Column2
OR
(Column1 <> Column2
AND
Column3 = Column4)
The Venn would look the same except the intersection would be white and excluded.
colum3 = colum4 only if doesnt exist any match for column1 = column2
– Juan Carlos Oropeza
Nov 14 '18 at 22:11
Yes. This covers it. If the first condition returns false then the second condition will be evaluated. If that is true then the whole thing returns true and the row is returned. The only rows excluded are ones where neither condition is true.
– Johnsonium
Nov 14 '18 at 22:14
doesnt work like that. the first condition has to return false for all rows before you can consider the second codition
– Juan Carlos Oropeza
Nov 14 '18 at 22:19
That isn't clear above. And even if it is you can simply modify the logic slightly: Column1 = Column2 OR (Column1 <> Column2 AND Column3 = Column4). The Venn would then have the intersection white and excluded.
– Johnsonium
Nov 14 '18 at 22:20
again, you need consider second condition if first condition fail for all rows. check my answer. I first calculate the count to see what case I have
– Juan Carlos Oropeza
Nov 14 '18 at 22:27
|
show 1 more comment
You can do this by using simple booleans:
WHERE
Column1 = Column2
OR
Column3 = Column4
The Venn diagram for this:

All of the shaded area will be returned, including when Column1 = Column2 & Column3 = Column4 (the dark shaded intersection)
If you don't want rows where both conditions are met then you can modify the logic:
WHERE
Column1 = Column2
OR
(Column1 <> Column2
AND
Column3 = Column4)
The Venn would look the same except the intersection would be white and excluded.
colum3 = colum4 only if doesnt exist any match for column1 = column2
– Juan Carlos Oropeza
Nov 14 '18 at 22:11
Yes. This covers it. If the first condition returns false then the second condition will be evaluated. If that is true then the whole thing returns true and the row is returned. The only rows excluded are ones where neither condition is true.
– Johnsonium
Nov 14 '18 at 22:14
doesnt work like that. the first condition has to return false for all rows before you can consider the second codition
– Juan Carlos Oropeza
Nov 14 '18 at 22:19
That isn't clear above. And even if it is you can simply modify the logic slightly: Column1 = Column2 OR (Column1 <> Column2 AND Column3 = Column4). The Venn would then have the intersection white and excluded.
– Johnsonium
Nov 14 '18 at 22:20
again, you need consider second condition if first condition fail for all rows. check my answer. I first calculate the count to see what case I have
– Juan Carlos Oropeza
Nov 14 '18 at 22:27
|
show 1 more comment
You can do this by using simple booleans:
WHERE
Column1 = Column2
OR
Column3 = Column4
The Venn diagram for this:

All of the shaded area will be returned, including when Column1 = Column2 & Column3 = Column4 (the dark shaded intersection)
If you don't want rows where both conditions are met then you can modify the logic:
WHERE
Column1 = Column2
OR
(Column1 <> Column2
AND
Column3 = Column4)
The Venn would look the same except the intersection would be white and excluded.
You can do this by using simple booleans:
WHERE
Column1 = Column2
OR
Column3 = Column4
The Venn diagram for this:

All of the shaded area will be returned, including when Column1 = Column2 & Column3 = Column4 (the dark shaded intersection)
If you don't want rows where both conditions are met then you can modify the logic:
WHERE
Column1 = Column2
OR
(Column1 <> Column2
AND
Column3 = Column4)
The Venn would look the same except the intersection would be white and excluded.
edited Nov 14 '18 at 22:26
answered Nov 14 '18 at 22:07
JohnsoniumJohnsonium
1,8621714
1,8621714
colum3 = colum4 only if doesnt exist any match for column1 = column2
– Juan Carlos Oropeza
Nov 14 '18 at 22:11
Yes. This covers it. If the first condition returns false then the second condition will be evaluated. If that is true then the whole thing returns true and the row is returned. The only rows excluded are ones where neither condition is true.
– Johnsonium
Nov 14 '18 at 22:14
doesnt work like that. the first condition has to return false for all rows before you can consider the second codition
– Juan Carlos Oropeza
Nov 14 '18 at 22:19
That isn't clear above. And even if it is you can simply modify the logic slightly: Column1 = Column2 OR (Column1 <> Column2 AND Column3 = Column4). The Venn would then have the intersection white and excluded.
– Johnsonium
Nov 14 '18 at 22:20
again, you need consider second condition if first condition fail for all rows. check my answer. I first calculate the count to see what case I have
– Juan Carlos Oropeza
Nov 14 '18 at 22:27
|
show 1 more comment
colum3 = colum4 only if doesnt exist any match for column1 = column2
– Juan Carlos Oropeza
Nov 14 '18 at 22:11
Yes. This covers it. If the first condition returns false then the second condition will be evaluated. If that is true then the whole thing returns true and the row is returned. The only rows excluded are ones where neither condition is true.
– Johnsonium
Nov 14 '18 at 22:14
doesnt work like that. the first condition has to return false for all rows before you can consider the second codition
– Juan Carlos Oropeza
Nov 14 '18 at 22:19
That isn't clear above. And even if it is you can simply modify the logic slightly: Column1 = Column2 OR (Column1 <> Column2 AND Column3 = Column4). The Venn would then have the intersection white and excluded.
– Johnsonium
Nov 14 '18 at 22:20
again, you need consider second condition if first condition fail for all rows. check my answer. I first calculate the count to see what case I have
– Juan Carlos Oropeza
Nov 14 '18 at 22:27
colum3 = colum4 only if doesnt exist any match for column1 = column2
– Juan Carlos Oropeza
Nov 14 '18 at 22:11
colum3 = colum4 only if doesnt exist any match for column1 = column2
– Juan Carlos Oropeza
Nov 14 '18 at 22:11
Yes. This covers it. If the first condition returns false then the second condition will be evaluated. If that is true then the whole thing returns true and the row is returned. The only rows excluded are ones where neither condition is true.
– Johnsonium
Nov 14 '18 at 22:14
Yes. This covers it. If the first condition returns false then the second condition will be evaluated. If that is true then the whole thing returns true and the row is returned. The only rows excluded are ones where neither condition is true.
– Johnsonium
Nov 14 '18 at 22:14
doesnt work like that. the first condition has to return false for all rows before you can consider the second codition
– Juan Carlos Oropeza
Nov 14 '18 at 22:19
doesnt work like that. the first condition has to return false for all rows before you can consider the second codition
– Juan Carlos Oropeza
Nov 14 '18 at 22:19
That isn't clear above. And even if it is you can simply modify the logic slightly: Column1 = Column2 OR (Column1 <> Column2 AND Column3 = Column4). The Venn would then have the intersection white and excluded.
– Johnsonium
Nov 14 '18 at 22:20
That isn't clear above. And even if it is you can simply modify the logic slightly: Column1 = Column2 OR (Column1 <> Column2 AND Column3 = Column4). The Venn would then have the intersection white and excluded.
– Johnsonium
Nov 14 '18 at 22:20
again, you need consider second condition if first condition fail for all rows. check my answer. I first calculate the count to see what case I have
– Juan Carlos Oropeza
Nov 14 '18 at 22:27
again, you need consider second condition if first condition fail for all rows. check my answer. I first calculate the count to see what case I have
– Juan Carlos Oropeza
Nov 14 '18 at 22:27
|
show 1 more comment
Another approach :
DECLARE
@t TABLE( ID INT, col1 CHAR(1), col2 CHAR(1), col3 INT, col4 INT)
INSERT INTO @t VALUES
(1234,'A','B',100,100),
(1234,'A','A',100,100),
(2468,'A','C',125,125),
(2468,'A','B',100,150)
SELECT
ID
, col1
, col2
, col3
, col4
FROM (
SELECT *, ROW_NUMBER() OVER(PARTITION BY ID, col1 ORDER BY col2) RN
FROM (
SELECT *
FROM @t
WHERE
col3 = col4
AND col1 = col2
UNION ALL
SELECT *
FROM @t
WHERE
col3 = col4
AND ID NOT IN(
SELECT ID
FROM @t
WHERE
col3 = col4
AND col1 = col2
)
) D
) E
WHERE
RN = 1
check this example rextester.com/HAMK64548 just change(A,A)for(C,C)and you wont get the desire output
– Juan Carlos Oropeza
Nov 15 '18 at 14:52
@JuanCarlosOropeza, I guess this will work as expected.
– iSR5
Nov 15 '18 at 16:42
add a comment |
Another approach :
DECLARE
@t TABLE( ID INT, col1 CHAR(1), col2 CHAR(1), col3 INT, col4 INT)
INSERT INTO @t VALUES
(1234,'A','B',100,100),
(1234,'A','A',100,100),
(2468,'A','C',125,125),
(2468,'A','B',100,150)
SELECT
ID
, col1
, col2
, col3
, col4
FROM (
SELECT *, ROW_NUMBER() OVER(PARTITION BY ID, col1 ORDER BY col2) RN
FROM (
SELECT *
FROM @t
WHERE
col3 = col4
AND col1 = col2
UNION ALL
SELECT *
FROM @t
WHERE
col3 = col4
AND ID NOT IN(
SELECT ID
FROM @t
WHERE
col3 = col4
AND col1 = col2
)
) D
) E
WHERE
RN = 1
check this example rextester.com/HAMK64548 just change(A,A)for(C,C)and you wont get the desire output
– Juan Carlos Oropeza
Nov 15 '18 at 14:52
@JuanCarlosOropeza, I guess this will work as expected.
– iSR5
Nov 15 '18 at 16:42
add a comment |
Another approach :
DECLARE
@t TABLE( ID INT, col1 CHAR(1), col2 CHAR(1), col3 INT, col4 INT)
INSERT INTO @t VALUES
(1234,'A','B',100,100),
(1234,'A','A',100,100),
(2468,'A','C',125,125),
(2468,'A','B',100,150)
SELECT
ID
, col1
, col2
, col3
, col4
FROM (
SELECT *, ROW_NUMBER() OVER(PARTITION BY ID, col1 ORDER BY col2) RN
FROM (
SELECT *
FROM @t
WHERE
col3 = col4
AND col1 = col2
UNION ALL
SELECT *
FROM @t
WHERE
col3 = col4
AND ID NOT IN(
SELECT ID
FROM @t
WHERE
col3 = col4
AND col1 = col2
)
) D
) E
WHERE
RN = 1
Another approach :
DECLARE
@t TABLE( ID INT, col1 CHAR(1), col2 CHAR(1), col3 INT, col4 INT)
INSERT INTO @t VALUES
(1234,'A','B',100,100),
(1234,'A','A',100,100),
(2468,'A','C',125,125),
(2468,'A','B',100,150)
SELECT
ID
, col1
, col2
, col3
, col4
FROM (
SELECT *, ROW_NUMBER() OVER(PARTITION BY ID, col1 ORDER BY col2) RN
FROM (
SELECT *
FROM @t
WHERE
col3 = col4
AND col1 = col2
UNION ALL
SELECT *
FROM @t
WHERE
col3 = col4
AND ID NOT IN(
SELECT ID
FROM @t
WHERE
col3 = col4
AND col1 = col2
)
) D
) E
WHERE
RN = 1
edited Nov 15 '18 at 16:41
answered Nov 15 '18 at 1:41
iSR5iSR5
1,578278
1,578278
check this example rextester.com/HAMK64548 just change(A,A)for(C,C)and you wont get the desire output
– Juan Carlos Oropeza
Nov 15 '18 at 14:52
@JuanCarlosOropeza, I guess this will work as expected.
– iSR5
Nov 15 '18 at 16:42
add a comment |
check this example rextester.com/HAMK64548 just change(A,A)for(C,C)and you wont get the desire output
– Juan Carlos Oropeza
Nov 15 '18 at 14:52
@JuanCarlosOropeza, I guess this will work as expected.
– iSR5
Nov 15 '18 at 16:42
check this example rextester.com/HAMK64548 just change
(A,A) for (C,C) and you wont get the desire output– Juan Carlos Oropeza
Nov 15 '18 at 14:52
check this example rextester.com/HAMK64548 just change
(A,A) for (C,C) and you wont get the desire output– Juan Carlos Oropeza
Nov 15 '18 at 14:52
@JuanCarlosOropeza, I guess this will work as expected.
– iSR5
Nov 15 '18 at 16:42
@JuanCarlosOropeza, I guess this will work as expected.
– iSR5
Nov 15 '18 at 16:42
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53309088%2fexecuting-if-statement-in-where-clause-sql-server%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
what you mean column1 = column2 doesnt exist?
– Juan Carlos Oropeza
Nov 14 '18 at 21:38
2
what's wrong with
WHERE Column1=Column2 OR Column3 = Column4?– scsimon
Nov 14 '18 at 21:39
2
Show us db schema, sample data, current and expected output. Please read How-to-Ask And here is a great place to START to learn how improve your question quality and get better answers. How to create a Minimal, Complete, and Verifiable example
– Juan Carlos Oropeza
Nov 14 '18 at 21:39
@scsimon My guess is if doesnt exist any rows with col1 = col2 then return rows where col3 = col4 but not sure
– Juan Carlos Oropeza
Nov 14 '18 at 21:39
1
@Johnsonium Your answer doesnt cover the first case. because
A <> Band100 = 100and the row shouldnt be on the result.– Juan Carlos Oropeza
Nov 14 '18 at 22:44