oracle_多字段统计
查询同一张表中同一字段的不同值的综合,方法如下:
select o.code 礼品代码,
o.name 礼品名称,
l.couponactivityid 券活动定义,
count(l.couponno) as 券总数量,
count(case
when l.state in ('0') then
'0'
end) 未兑换券数量,
count(case
when l.state in ('1') then
'1'
end) 已兑换未核销券数量,
count(case
when l.state in ('2') then
'2'
end) 已核销券数量
from css_coupon l, css_award o
where l.couponactivityid = o.couponcode
and o.state = '正常'
group by o.code, o.name, l.couponactivityid
用法解析:
count(case
when l.state in ('0') then
'0'
end) 未兑换券数量,
count(case
when l.state in ('1') then
'1'
end) 已兑换未核销券数量,
count(case
when l.state in ('2') then
'2'
end) 已核销券数量
上述中有三个count,分别查询同一个字段l.state ,但是对这一个字段的查询值是不同的,即 in ('0') in ('1') in ('2') 算法等同于,='0' ='1' ='2'
此处简单记录一下,有兴趣的话可以搜索一下case的用法