在'case'表达式中使用'in'语句

时间:2016-03-08 05:51:46

标签: sql sql-server

如何在我的“案例表达”中使用int

这是我的查询,如果找到IN之类的任何匹配作为mumbai,我想要计数:

mumbai

2 个答案:

答案 0 :(得分:1)

你很接近,你只需要用IN逗号分隔值而不是ORCASE表达式有两种格式,在{WHEN后面加上标准的字段{1}}更灵活,所以我更喜欢单独使用它:

SELECT TYPEOFPRODUCT
      ,SUM( CASE WHEN BRANCH IN('Mumbai1','Mumbai2') THEN 1 ELSE 0 END) AS Mumbai
      ,SUM( CASE WHEN BRANCH IN('Delhi1', 'DelhiMain') THEN 1 ELSE 0 END) AS Delhi
FROM los_ext  
GROUP BY TYPEOFPRODUCT

答案 1 :(得分:0)

其查询选择各个分支的总和

select TYPEOFPRODUCT,
SUM(CASE WHEN (BRANCH = 'Mumbai1' OR BRANCH = 'Mumbai2' ) THEN 1 ELSE 0 END) AS 'Mumbai',
SUM(CASE WHEN (BRANCH = 'Delhi1' OR BRANCH = 'DelhiMain' )  THEN 1 ELSE 0 END) AS 'Delhi'
from los_ext  
group by TYPEOFPRODUCT