因此,我的任务是计算一些简单的KPI。
我已经积累了所需的所有数据的视图。
Year_CW Is Started Needs Help
-------------------------------------
2018/45 0 1
2018/43 1 1
2018/45 0 1
2018/42 1 0
2018/45 0 1
2018/45 1 1
2018/41 0 1
2018/43 0 0
2018/45 1 1
2018/45 0 0
然后,我编写了以下查询:
SELECT DISTINCT YEAR_CW
FROM TestView
ORDER BY YEAR_CW DESC
哪个返回
Year_CW
--------
2018/45
2018/44
2018/43
2018/42
现在,我想为每个Year_CW计数其他两行分别为1和0的频率。这可能是一个相当简单的问题,但是我只是从SQL开始,我真的不知道基于外部查询的查询的关键字是什么。
其他查询将是
Select Count(Is Started)
from Testview
Where Is Started = 1
以此类推。但是我真的不知道如何将它们放在一起并基于第一个查询。
谢谢你的帮助。
您似乎想要条件聚合:
select Year_CW,
sum(case when col = 1 then 1 else 0 end) as one_count,
sum(case when col = 0 then 1 else 0 end) as zero_count
from (select Year_CW, IsStarted as col
from TestView tv
union all
select Year_CW, NeedsHelp
from TestView tv
) tv
group by Year_CW
order by Year_CW desc;
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句