记一次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中拼接起来。
阅读全文
0 0
- 记一次SQL查询
- 记一次SQL查询优化
- 记一次sql慢查询
- 记一次sql优化,in+子查询
- 记一次sql查询利用不算艰难曲折渗透的过程
- 记一次苦逼的Sql查询优化
- 记一次苦逼的SQL查询优化
- SQL优化一则:CBO查询SQL一次比一次慢
- sql 查询最近一次日期的访问
- 关联查询SQL的一次优化过程
- 一次sql查询优化 并行度
- 记一次MySQL查询优化
- 记一次慢查询优化
- 记一次 SQL SERVER 数据库查询性能优化 千万级数据
- 记一次sql server 性能调优,查询从20秒至2秒
- 记一次sql优化,or查询优化与连接索引失效
- 生成规定时间段内随机日期——记一次SQL查询
- 一次查询
- Java连接数据库
- dell 成就 5470安装win10 ubuntu双系统方法
- python(三)
- 循环计算文本框值之间的差值
- Opencv-python接口:轮廓检测
- 记一次SQL查询
- 进程与线程-2、用户级线程切换过程
- uva 1404 求很大数的素数算法
- 网络流量监控告警
- AspectJ学习总结
- Dialog中存在radiogroup动态添加radiobutton的ID问题
- Struts2 如何用JS获取sx:datetimepicker选择的日期
- Android Drawable 和 xml文件转化关系
- CentOS 7 安装 ShadowSocks 客户端