简体   繁体   中英

SQL Case with multiple values

Is there a condensed form of the following statement?

SELECT Name, Case StatusID WHEN 1 THEN 'Alive' WHEN 2 THEN 'Alive' WHEN 3 THEN 'Alive' WHEN 4 THEN 'Dying' ELSE 'Dead' END FROM People

for example

CASE StatusID WHEN 1,2,3 THEN 'Alive'

or

CASE StatusID WHEN 1 OR 2 OR 3 THEN 'Alive'

depending on the DB you use the following will do the trick

SELECT 
Name, 
Case WHEN StatusID  IN ( 1, 2, 3 ) THEN 'Alive' WHEN StatusID = 4 THEN 'Dying' ELSE 'Dead' END 
FROM People

In Oracle, assuming statuid is never <= 0:

SELECT Name, CASE WHEN statusid < 4 THEN 'Alive'
                  WHEN statusid = 4 THEN 'Dying'
                  ELSE 'Dead' END AS some_alias
  FROM people

You can also use DECODE.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM