记一次SQL查询

来源:互联网 发布:js 获取html属性值 编辑:程序博客网 时间:2024/06/05 22:57

下面是某项目中某功能的一条SQL命令

目标效果大概如下

这里写图片描述

下面是部分实现代码(其中ID列可以删除)

select  g.name,g.id, sum(case             when t.household_county_id = g.id                then              1             else              0           end) as 户籍人数,  sum(case             when t.residence_county_id = 4 and t.household_county_id = g.id                then              1             else              0           end) as 朝阳区,  sum(case             when t.residence_county_id = 8 and t.household_county_id = g.id                then              1             else              0           end) as 海淀区,  sum(case             when t.residence_county_id = 301 and t.household_county_id = g.id                then              1             else              0           end) as 丰台区,  sum(case             when t.residence_county_id = 461 and t.household_county_id = g.id                then              1             else              0           end) as 东城区,  sum(case             when t.residence_county_id = 462 and t.household_county_id = g.id                then              1             else              0           end) as 西城区,  sum(case             when t.residence_county_id = 466 and t.household_county_id = g.id                then              1             else              0           end) as 通州区,  sum(case             when t.residence_county_id = 467 and t.household_county_id = g.id                then              1             else              0           end) as 石景山区,  sum(case             when t.residence_county_id = 468 and t.household_county_id = g.id                then              1             else              0           end) as 大兴区,  sum(case             when t.residence_county_id = 469 and t.household_county_id = g.id                then              1             else              0           end) as 房山区,  sum(case             when t.residence_county_id = 470 and t.household_county_id = g.id                then              1             else              0           end) as 门头沟区,  sum(case             when t.residence_county_id = 471 and t.household_county_id = g.id                then              1             else              0           end) as 平谷区,  sum(case             when t.residence_county_id = 472 and t.household_county_id = g.id                then              1             else              0           end) as 密云区,             sum(case             when t.residence_county_id = 473 and t.household_county_id = g.id                then              1             else              0           end) as 怀柔区,  sum(case             when t.residence_county_id = 474 and t.household_county_id = g.id                then              1             else              0           end) as 延庆区,  sum(case             when t.residence_county_id = 475 and t.household_county_id = g.id                then              1             else              0           end) as 昌平区,              sum(case             when t.residence_county_id = 476 and t.household_county_id = g.id                then              1             else              0           end) as 顺义区                      from AG_BA_OLD_BASE t left join gos_region g on t.household_county_id = g.idwhere t.is_nonlocal<>2205 and t.make_card_success=2205  and t.is_death <> 2205and (to_char(t.back_in_time,'yyyy-MM-dd') ) <= '2017-03-31' group by g.name,g.id,g.standard_noorder by g.standard_no

这里写图片描述

本条sql只涉及两个表,核心就是sum case when的应用

另外需要注意的几点就是关联时 on 后加的条件一定要注意,因为左边一列的字段意思为户籍所在区 所以条件要为

t.household_county_id = g.id

在每列中,切记不要忘记加上

t.household_county_id = g.id

这两个位置如果弄混了,那么行和列的意义将会颠倒。

另外还有

“居住人数”的统计最好放在SQL外,比如java程序来实现比较方便
非要用sql操作需要借用union 比较麻烦

还有目标表中最后三行的前两行可以分别用两个sql来实现 在jsp中拼接起来。