Loading

sql group by sum

 

1.表字段如下图,要求:分区计算总处数,总面积,已处理数,已处理面积,未处理数,未处理面积

 

 2.计算结果如下:

 

 3.sql如下:知识点:sum后的case可以根据A列状态,来计算B列的值。

sum(case when sfcl='否'then mjm else 0 end) wcltbmj

 

 1 select max(xzqmc2) qymc,0 as hzbs,
 2 sum(mjm) as bhmj,
 3 count(sfcl) as bhs,
 4 sum(case when sfcl='是'then 1 else 0 end) ycltbs ,
 5 sum(case when sfcl='是'then mjm else 0 end) ycltbmj,
 6 sum(case when sfcl='否'then 1 else 0 end) wcltbs ,
 7 sum(case when sfcl='否'then mjm else 0 end) wcltbmj
 8 from zz_ygyswftb where pcsid = #{pcid} group by xzqmc2
 9 union
10 select '萍乡' qymc,1 as hzbs,
11 sum(mjm) as bhmj,
12 count(sfcl) as bhs,
13 sum(case when sfcl='是'then 1 else 0 end) ycltbs ,
14 sum(case when sfcl='是'then mjm else 0 end) ycltbmj,
15 sum(case when sfcl='否'then 1 else 0 end) wcltbs ,
16 sum(case when sfcl='否'then mjm else 0 end) wcltbmj
17 from zz_ygyswftb where pcsid = #{pcid}

 

posted @ 2021-02-03 10:16  风萧萧5  阅读(786)  评论(0编辑  收藏  举报