July 24, 2013 at 3:35 pm
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
July 24, 2013 at 4:01 pm
This would do the trick:
SELECT
CASE
WHEN ISNULL(rate,100)=100 THEN 1
ELSE 0
END AS Rate
FROM {yourtable}
-- Itzik Ben-Gan 2001
July 24, 2013 at 4:03 pm
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
July 24, 2013 at 4:19 pm
Thanks both of you
July 24, 2013 at 6:44 pm
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 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?[/url]
Since random numbers are too important to be left to chance, let's generate some![/url]
Learn to understand recursive CTEs by example.[/url]
[url url=http://www.sqlservercentral.com/articles/St
July 26, 2013 at 4:58 pm
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
Change is inevitable... Change for the better is not.
July 28, 2013 at 7:15 pm
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 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?[/url]
Since random numbers are too important to be left to chance, let's generate some![/url]
Learn to understand recursive CTEs by example.[/url]
[url url=http://www.sqlservercentral.com/articles/St
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply
This website stores cookies on your computer.
These cookies are used to improve your website experience and provide more personalized services to you, both on this website and through other media.
To find out more about the cookies we use, see our Privacy Policy