sum和case

来源:互联网 发布:乔丹各赛季数据统计 编辑:程序博客网 时间:2024/05/22 23:27

近期写SQL统计,用到了SUM,还有case,用得麻溜麻溜的。
自我欣赏一下:

select 所在重点区域,sum(case b when 1 then 1 else 0 end) Builded,sum(case b when 0 then 1 else 0 end) Building,sum(case b when -1 then 1 else 0 end) Willfrom(select 所在重点区域,1 b from sde.station_gh where 建设状况='已建'union allselect 所在重点区域,0 b from sde.station_gh where 建设状况='在建'union allselect 所在重点区域,-1 b from sde.station_gh where 建设状况='拟建') tgroup by 所在重点区域select 监测目标,sum(case b when 1 then 1 else 0 end) Builded,sum(case b when 0 then 1 else 0 end) Building,sum(case b when -1 then 1 else 0 end) Willfrom(select 监测目标,1 b from sde.station_gh where 建设状况='已建'union allselect 监测目标,0 b from sde.station_gh where 建设状况='在建'union allselect 监测目标,-1 b from sde.station_gh where 建设状况='拟建') tgroup by 监测目标
原创粉丝点击