记一次SQL查询

下面是某项目中某功能的一条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.id
where 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_no
order 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中拼接起来。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值