SQL中case when的用法

SQL中case when的用法

select b ,(case when count(b)=sum(case when a=null then 0 else 1 end) then 0 else count(b) end) from 表名 group by b
(A B 两个字段, 在B相同的情况下, 要对A有条件的记数,即只要A有一个非空, 就全部记数; A都为空, 也全部记数; A都不为空, 则都不记数

举例:

1. A B
* V1
V1 (记数结果 V1: 2)

2. A B
V1
V1 (记数结果 V1: 2)

3. A B
* V1
* V1 (记数结果 V1: 0)
)
温馨提示:内容为网友见解,仅供参考
第1个回答  2019-11-23
给你个例子
select
rq,case
deptid
when
'0201'
then
'开发区店'
when
'0202'
then
'金州店'
when
'0203'
then
'华南南店'
end
fd
from
ghdwjxcr
group
by
rq,substring(deptid,1,4)
第2个回答  2019-12-07
CASE
WHEN要么是查询条件要么是查询结构,表名逗号后面没用,语法不对
第3个回答  2009-08-20
case '字段' when 1 then '你好' when 2 then
'hi' else '88' end
第4个回答  2009-08-21
case '字段' when 1 then '你好' when 2 then
'hi' else '88' end
相似回答