我有一张这样的桌子:
Name Source ended_status date Environment
House DC 1 2019/10/03 Pro
Cat DC2 1 2019/10/05 Pro
Pen DC 1 2019/10/03 Pro
Pen DC 0 2019/11/07 Pre我想要得到:
Source Environment Ended_Status_with_1 Ended_Status_with_2
DC Pro 2 0
DC Pre 1 0
DC2 Pro 1 0因此,它们必须按源、环境进行分组,我必须计算它们的所有Ended_Status与1和所有结束状态2的总和,并将其放在同一行中。
我怎么能这么做呢?
我可以进行按每个ended_status分组的查询,但不能将ended的两个求和放在同一行。
非常感谢和抱歉,我的英语很差!
发布于 2019-10-31 22:10:58
您可以使用条件聚合:
select
source,
environment,
sum(case when ended_status = 1 then 1 else 0 end) ended_status_with_1,
sum(case when ended_status = 2 then 1 else 0 end) ended_status_with_2
from mytable
group by
source,
environment发布于 2019-11-02 17:52:07
另外,尝试下面的查询
select source,environment,sum(decode(ended_status,1,1,0)) ended_status_with_1,
sum(decode(ended_status,2,1,0)) ended_status_with_2 from mytable
group by source,environmenthttps://stackoverflow.com/questions/58645153
复制相似问题