SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Case statement with <> condition


Case statement with <> condition

Author
Message
Megha P
Megha P
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1154 Visits: 2501
I have query something like this ..i haven't posted here original quary
   IF (@p_flag = 1)
BEGIN
SELECT ..
...
FROM Test1 T1
INNER JOIN Test2 T2 ON T1.ID = T2.ID
WHERE T.moveFlag = 26
END
ELSE
BEGIN
SELECT ..
...
FROM Test1 T1
INNER JOIN Test2 T2 ON T1.ID = T2.ID
WHERE T.moveFlag <> 26
END



I come up with this
 SELECT ..
...
FROM Test1 T1
INNER JOIN Test2 T2 ON T1.ID = T2.ID
WHERE 1 = CASE WHEN @p_flag = 1
THEN CASE WHEN moveFlag = 26
THEN 1
ELSE 0
END
ELSE CASE WHEN moveFlag <> 26
THEN 1
ELSE 0
END
END


Any other optimized way to make this in one statement?
Sean Lange
Sean Lange
SSC Guru
SSC Guru (149K reputation)SSC Guru (149K reputation)SSC Guru (149K reputation)SSC Guru (149K reputation)SSC Guru (149K reputation)SSC Guru (149K reputation)SSC Guru (149K reputation)SSC Guru (149K reputation)

Group: General Forum Members
Points: 149305 Visits: 18575
How about this?


SELECT [Columns]
FROM Test1 T1
INNER JOIN Test2 T2 ON T1.ID = T2.ID
WHERE CASE @p_flag
WHEN 1 THEN moveFlag = 26
ELSE moveFlag > 26 OR moveFlag < 26
END



Notice I modified the second predicate to two portions. This is to allow any index on moveFlag to be a seek instead of a scan.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Lowell
Lowell
SSC Guru
SSC Guru (184K reputation)SSC Guru (184K reputation)SSC Guru (184K reputation)SSC Guru (184K reputation)SSC Guru (184K reputation)SSC Guru (184K reputation)SSC Guru (184K reputation)SSC Guru (184K reputation)

Group: General Forum Members
Points: 184023 Visits: 41569
well, the WHERE statement is still a "catch all" query, but isn't this functionally identical?


WHERE 1 = CASE WHEN @p_flag = 1 OR moveFlag = 26
THEN 1
ELSE 0
END



Lowell
--help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!
Sean Lange
Sean Lange
SSC Guru
SSC Guru (149K reputation)SSC Guru (149K reputation)SSC Guru (149K reputation)SSC Guru (149K reputation)SSC Guru (149K reputation)SSC Guru (149K reputation)SSC Guru (149K reputation)SSC Guru (149K reputation)

Group: General Forum Members
Points: 149305 Visits: 18575
Lowell (5/7/2014)
well, the WHERE statement is still a "catch all" query, but isn't this functionally identical?


WHERE 1 = CASE WHEN @p_flag = 1 OR moveFlag = 26
THEN 1
ELSE 0
END



Wouldn't this not return any rows when @p_flag = 0? This would eliminate all the rows in the original second query.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
David Burrows
David Burrows
SSC-Dedicated
SSC-Dedicated (31K reputation)SSC-Dedicated (31K reputation)SSC-Dedicated (31K reputation)SSC-Dedicated (31K reputation)SSC-Dedicated (31K reputation)SSC-Dedicated (31K reputation)SSC-Dedicated (31K reputation)SSC-Dedicated (31K reputation)

Group: General Forum Members
Points: 31938 Visits: 10706
Well subject to indexes and performance testing I do this

;with cte (ID) AS (
SELECT ID
FROM Test1 T1
WHERE @p_flag = 1
AND T1.moveFlag = 26
UNION ALL
SELECT ID
FROM Test1 T1
WHERE @p_flag = 0
AND T1.moveFlag <> 26)
SELECT ..
FROM cte T1
JOIN Test2 T2 ON T1.ID = T2.ID




Far away is close at hand in the images of elsewhere.

Anon.


gbritton1
gbritton1
Hall of Fame
Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)

Group: General Forum Members
Points: 3373 Visits: 966
Sean Lange (5/7/2014)
How about this?


SELECT [Columns]
FROM Test1 T1
INNER JOIN Test2 T2 ON T1.ID = T2.ID
WHERE CASE @p_flag
WHEN 1 THEN moveFlag = 26
ELSE moveFlag > 26 OR moveFlag < 26
END



how did you make this work? I copied and pasted this into SSMS, set up test tables but it won't parse. I get:

[quote]Msg 102, Level 15, State 1, Line 3
Incorrect syntax near '='.

Sean Lange
Sean Lange
SSC Guru
SSC Guru (149K reputation)SSC Guru (149K reputation)SSC Guru (149K reputation)SSC Guru (149K reputation)SSC Guru (149K reputation)SSC Guru (149K reputation)SSC Guru (149K reputation)SSC Guru (149K reputation)

Group: General Forum Members
Points: 149305 Visits: 18575
gbritton1 (5/7/2014)
Sean Lange (5/7/2014)
How about this?


SELECT [Columns]
FROM Test1 T1
INNER JOIN Test2 T2 ON T1.ID = T2.ID
WHERE CASE @p_flag
WHEN 1 THEN moveFlag = 26
ELSE moveFlag > 26 OR moveFlag < 26
END



how did you make this work? I copied and pasted this into SSMS, set up test tables but it won't parse. I get:

[quote]Msg 102, Level 15, State 1, Line 3
Incorrect syntax near '='.



Ahh you are correct. It will not work as posted. This is some fallout from not having ddl and sample data to work with.

This should work...of course it is untested for the same reason I didn't test my previous post.


WHERE (@p_flag = 1 AND moveFlag = 26)
OR (@p_flag <> 1 --if this is a bit I would use = 0 instead of <> 1
AND moveFlag > 26 OR moveFlag < 26)



This is similar to a catch-all type of query. Gail has an excellent article about that type of query here. http://sqlinthewild.co.za/index.php/2009/03/19/catch-all-queries/

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
gbritton1
gbritton1
Hall of Fame
Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)

Group: General Forum Members
Points: 3373 Visits: 966
Sean Lange (5/7/2014)
gbritton1 (5/7/2014)
Sean Lange (5/7/2014)
How about this?


SELECT [Columns]
FROM Test1 T1
INNER JOIN Test2 T2 ON T1.ID = T2.ID
WHERE CASE @p_flag
WHEN 1 THEN moveFlag = 26
ELSE moveFlag > 26 OR moveFlag < 26
END



how did you make this work? I copied and pasted this into SSMS, set up test tables but it won't parse. I get:

[quote]Msg 102, Level 15, State 1, Line 3
Incorrect syntax near '='.



Ahh you are correct. It will not work as posted. This is some fallout from not having ddl and sample data to work with.

This should work...of course it is untested for the same reason I didn't test my previous post.


WHERE (@p_flag = 1 AND moveFlag = 26)
OR (@p_flag <> 1 --if this is a bit I would use = 0 instead of <> 1
AND moveFlag > 26 OR moveFlag < 26)



This is similar to a catch-all type of query. Gail has an excellent article about that type of query here. http://sqlinthewild.co.za/index.php/2009/03/19/catch-all-queries/



...might want parens arount that inner OR condition, I spose
Sean Lange
Sean Lange
SSC Guru
SSC Guru (149K reputation)SSC Guru (149K reputation)SSC Guru (149K reputation)SSC Guru (149K reputation)SSC Guru (149K reputation)SSC Guru (149K reputation)SSC Guru (149K reputation)SSC Guru (149K reputation)

Group: General Forum Members
Points: 149305 Visits: 18575
gbritton1 (5/7/2014)
Sean Lange (5/7/2014)
gbritton1 (5/7/2014)
Sean Lange (5/7/2014)
How about this?


SELECT [Columns]
FROM Test1 T1
INNER JOIN Test2 T2 ON T1.ID = T2.ID
WHERE CASE @p_flag
WHEN 1 THEN moveFlag = 26
ELSE moveFlag > 26 OR moveFlag < 26
END



how did you make this work? I copied and pasted this into SSMS, set up test tables but it won't parse. I get:

[quote]Msg 102, Level 15, State 1, Line 3
Incorrect syntax near '='.



Ahh you are correct. It will not work as posted. This is some fallout from not having ddl and sample data to work with.

This should work...of course it is untested for the same reason I didn't test my previous post.


WHERE (@p_flag = 1 AND moveFlag = 26)
OR (@p_flag <> 1 --if this is a bit I would use = 0 instead of <> 1
AND moveFlag > 26 OR moveFlag < 26)



This is similar to a catch-all type of query. Gail has an excellent article about that type of query here. http://sqlinthewild.co.za/index.php/2009/03/19/catch-all-queries/



...might want parens arount that inner OR condition, I spose


Only if we want it to be correct. ;-) Good catch!!!

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Megha P
Megha P
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1154 Visits: 2501
Sean Lange's code works perfectly ..with index seek

WHERE (@p_flag = 1 AND moveFlag = 26)
OR (@p_flag <> 1 --if this is a bit I would use = 0 instead of <> 1
AND moveFlag > 26 OR moveFlag < 26)

Thank you all...
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum








































































































































































SQLServerCentral


Search