码迷,mamicode.com
首页 > 数据库 > 详细

sql group by sum

时间:2021-02-04 11:42:00      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:分区   rgb   ase   技术   sql   case when   where   lazy   根据   

 

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}

 

sql group by sum

标签:分区   rgb   ase   技术   sql   case when   where   lazy   根据   

原文地址:https://www.cnblogs.com/fxx5/p/14365576.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!