Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Case statement Expand / Collapse
Author
Message
Posted Wednesday, July 24, 2013 3:35 PM
SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Wednesday, December 10, 2014 10:06 AM
Points: 1,792, Visits: 3,261
I would like use case statement in select .for example, if rate =100, then 1, if it is null, then 1, and if it is <>100, it is 0,

how to write the select case statement in best way?

Thanks
Post #1477296
Posted Wednesday, July 24, 2013 4:01 PM


SSChasing Mays

SSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing Mays

Group: General Forum Members
Last Login: Today @ 8:59 AM
Points: 646, Visits: 2,992
This would do the trick:
SELECT 
CASE
WHEN ISNULL(rate,100)=100 THEN 1
ELSE 0
END AS Rate
FROM {yourtable}



-- Alan Burstein



Read this article for best practices on asking questions.
Need to split a string? Try this (Jeff Moden)
Need a pattern-based string spitter? Try this (Dwain Camps)

"I can't stress enough the importance of switching from a 'sequential files' mindset to 'set-based' thinking. After you make the switch, you can spend your time tuning and optimizing your queries instead of maintaining lengthy, poor-performing code. " -- Itzek Ben-Gan 2001

My blog
Post #1477310
Posted Wednesday, July 24, 2013 4:03 PM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Sunday, September 29, 2013 1:24 AM
Points: 429, Visits: 1,721
sqlfriends (7/24/2013)
I would like use case statement in select .for example, if rate =100, then 1, if it is null, then 1, and if it is <>100, it is 0,

how to write the select case statement in best way?

Thanks



SELECT
(CASE
WHEN rate IS NULL THEN 1
WHEN rate = 100 THEN 1
WHEN rate <> 100 THEN 0
END)
AS RateCode


Post #1477311
Posted Wednesday, July 24, 2013 4:19 PM
SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Wednesday, December 10, 2014 10:06 AM
Points: 1,792, Visits: 3,261
Thanks both of you
Post #1477314
Posted Wednesday, July 24, 2013 6:44 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Yesterday @ 4:49 PM
Points: 3,427, Visits: 5,378
Why use a CASE at all?

WITH Rates (rate) AS (
SELECT -100 UNION ALL SELECT NULL UNION ALL SELECT 0
UNION ALL SELECT 100 UNION ALL SELECT 5)
SELECT rate, 1-ABS(SIGN(100-ISNULL(rate,100)))
FROM Rates





My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
Post #1477327
Posted Friday, July 26, 2013 4:58 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 2:42 PM
Points: 35,768, Visits: 32,431
dwain.c (7/24/2013)
Why use a CASE at all?

WITH Rates (rate) AS (
SELECT -100 UNION ALL SELECT NULL UNION ALL SELECT 0
UNION ALL SELECT 100 UNION ALL SELECT 5)
SELECT rate, 1-ABS(SIGN(100-ISNULL(rate,100)))
FROM Rates




Just one reason but not the reason most who know me might think. On a million rows, the CASE method takes about 187ms (after optimizing for the "most common first" order , 234ms un-optimized) and the FORMULA comes in at about 218ms. While that's about 1/6th slower than the optimized CASE method, that's not the reason why I'd say to use the CASE statement. I say it's simply because the CASE statement will be easier to maintain by an individual not having quite as much formula knowledge. If the formula were MUCH faster across the million rows then, of course, I'd go with the formula and a comment explaining how it worked.


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1478246
Posted Sunday, July 28, 2013 7:15 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Yesterday @ 4:49 PM
Points: 3,427, Visits: 5,378
Jeff Moden (7/26/2013)
dwain.c (7/24/2013)
Why use a CASE at all?

WITH Rates (rate) AS (
SELECT -100 UNION ALL SELECT NULL UNION ALL SELECT 0
UNION ALL SELECT 100 UNION ALL SELECT 5)
SELECT rate, 1-ABS(SIGN(100-ISNULL(rate,100)))
FROM Rates




Just one reason but not the reason most who know me might think. On a million rows, the CASE method takes about 187ms (after optimizing for the "most common first" order , 234ms un-optimized) and the FORMULA comes in at about 218ms. While that's about 1/6th slower than the optimized CASE method, that's not the reason why I'd say to use the CASE statement. I say it's simply because the CASE statement will be easier to maintain by an individual not having quite as much formula knowledge. If the formula were MUCH faster across the million rows then, of course, I'd go with the formula and a comment explaining how it worked.


Buzz-kill!

But you're right of course.



My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
Post #1478425
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse