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

Excel IIF to SQL Expand / Collapse
Author
Message
Posted Thursday, April 3, 2014 1:18 PM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Yesterday @ 1:01 PM
Points: 215, Visits: 639
I need to create a query in SQL for an Excel statement which goes like ..

Check status , If its Yes then 'WithinStatus'
If it is not yes then check Hours and if Hours is < Benchmark then 'OutsideStatus' otherwise InsideStatus



I started with Case statement but then I am confused...

Case when Status = 'Yes' then 'WithinStatus'
Else
Case when Hours< Benchmark Then 'OutsideStatus'
Else
'WithinStatus'
End as StatusType.

Don't know how to go with the other Else....

Post #1558207
Posted Thursday, April 3, 2014 1:24 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 @ 10:33 PM
Points: 3,374, Visits: 7,296
You don't need several CASE, one is enough.
Case when Status = 'Yes' then 'WithinStatus'
when Hours< Benchmark Then 'OutsideStatus'
Else 'WithinStatus'
End as StatusType.

If a condition evaluates to true, it won't evaluate the following possibilities.



Luis C.
I am a great believer in luck, and I find the harder I work the more I have of it. Stephen Leacock

Forum Etiquette: How to post data/code on a forum to get the best help
Post #1558218
Posted Thursday, April 3, 2014 1:31 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 4:14 PM
Points: 13,126, Visits: 11,964
Luis Cazares (4/3/2014)
You don't need several CASE, one is enough.
Case when Status = 'Yes' then 'WithinStatus'
when Hours< Benchmark Then 'OutsideStatus'
Else 'WithinStatus'
End as StatusType.

If a condition evaluates to true, it won't evaluate the following possibilities.


Luis is absolutely correct here. He made the same oversight that you did originally though. Your original post (although over complicated) would work. The last condition in both of our code should be 'InsideStatus'

Case when Status = 'Yes' then 'WithinStatus'
when Hours< Benchmark Then 'OutsideStatus'
Else 'InsideStatus'
End as StatusType




_______________________________________________________________

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 Moden's 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)
Post #1558225
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse