结合这两种情况时的陈述

时间:2014-07-07 14:30:04

标签: sql-server

以下案例陈述有效,返回Yes,col1的值= 1。如何在col2

中包含第二个
SELECT CASE 
WHEN col1= '1' THEN 'Yes' ELSE 'No' END 
FROM PROVIDERS
WHERE NAME = 'Hospital1'

以下给出了错误

 SELECT CASE 
(WHEN col1= '1' THEN 'Yes' ELSE 'No' END) as 1st,
(WHEN col2= '1' THEN 'Yes' ELSE 'No' END) as 2nd
    FROM PROVIDERS
WHERE NAME = 'Hospital1'

2 个答案:

答案 0 :(得分:2)

第二栏

错过了

CASE

SELECT 
       CASE WHEN col1 = '1' THEN 'Yes' ELSE 'No' END AS 1st,
       CASE WHEN col2 = '1' THEN 'Yes' ELSE 'No' END AS 2nd
FROM   PROVIDERS
WHERE  NAME = 'Hospital1'

答案 1 :(得分:2)

格式正确:

SELECT CASE 
        WHEN col1 = '1'
            THEN 'Yes'
        ELSE 'No'
        END AS 1st
    ,CASE 
        WHEN col2 = '1'
            THEN 'Yes'
        ELSE 'No'
        END AS 2nd
FROM PROVIDERS
WHERE NAME = 'Hospital1'