CASE WHEN的两种表达形式:
方式一:CASEWHEN D10='成功签收' THEN 1WHEN D11='成功签收' THEN 1ELSE NULL END WHEN 中间是用判断表达式方式二: CASE D10WHEN '成功签收' THEN 1WHEN '运输途中' THEN 2ELSE '其他状态' END
方式一适合对一条记录中的多列进行判断。
方式二适合对一列中的多个条记录进行判断。
CASE WHEN在不同语句位置的用法
SELECT CASE WHEN 用法
SELECT COUNTRY, COUNT(CASE WHEN D10 = '成功签收' THEN 1 ELSE NULL END) D10, COUNT(CASE WHEN D11 = '成功签收' THEN 1 ELSE NULL END) D11, COUNT(CASE WHEN D12 = '成功签收' THEN 1 ELSE NULL END) D12, COUNT(CASE WHEN D13 = '成功签收' THEN 1 ELSE NULL END) D13, COUNT(CASE WHEN D14 = '成功签收' THEN 1 ELSE NULL END) D14, COUNT(CASE WHEN D15 = '成功签收' THEN 1 ELSE NULL END) D15FROM ......
WHERE CASE WHEN 用法
SELECT * FROMWHERE(CASE WHEN expr1 = 'search1' AND expr2 IN ('search2', 'search3') THEN 1 WHEN expr3 IN ('search4', 'search5') THEN 2 ELSE 0 END) = 1
GROUP BY CASE WHEN 用法
SELECT CASE WHEN SALARY <= 500 THEN '1' WHEN SALARY > 500 AND SALARY <= 600 THEN '2' WHEN SALARY > 600 AND SALARY <= 800 THEN '3' WHEN SALARY > 800 AND SALARY <= 1000 THEN '4' ELSE NULL END SALARY_CLASS, COUNT(*) FROM SALARY GROUP BY CASE WHEN SALARY <= 500 THEN '1' WHEN SALARY > 500 AND SALARY <= 600 THEN '2' WHEN SALARY > 600 AND SALARY <= 800 THEN '3' WHEN SALARY > 800 AND SALARY <= 1000 THEN '4' ELSE NULL END;
参考: