SQL 权威指南
SQL 解惑
在进行非定制化统计时,需要将已有编号方式转换为另外一种便于分析的方式进行统计需求

select
case
when name='哈尔滨' then '黑龙江'
when name='大庆' then '黑龙江'
when name='齐齐哈尔' then '黑龙江'
when name='长春' then '吉林'
when name='吉林' then '吉林'
when name='沈阳' then '辽宁'
when name='大连' then '辽宁'
when name='葫芦岛' then '辽宁'
else '其他'
end as province,sum(population)
from city_population
GROUP BY
case
when name='哈尔滨' then '黑龙江'
when name='大庆' then '黑龙江'
when name='齐齐哈尔' then '黑龙江'
when name='长春' then '吉林'
when name='吉林' then '吉林'
when name='沈阳' then '辽宁'
when name='大连' then '辽宁'
when name='葫芦岛' then '辽宁'
else '其他' end

select
case
when name='哈尔滨' then '黑龙江'
when name='大庆' then '黑龙江'
when name='齐齐哈尔' then '黑龙江'
when name='长春' then '吉林'
when name='吉林' then '吉林'
when name='沈阳' then '辽宁'
when name='大连' then '辽宁'
when name='葫芦岛' then '辽宁'
else '其他'
end as province,sum(population)
from city_population
group by province

严格来说,这种写法违反标准sql规范,因为Group by 比select 语句先知,
所以group by 子句中引用在select 子句里定义的别称是不被允许的,
事实上,在oracle、DB2、SQLServer 等数据库采用这种写法时就会出错

将人口按照适当的级别进行分类统计
select
case
when population < 100 then '1'
when population >=100 and population <200 then '2'
when population >=200 and population < 400 then '3'
else '4' end as pop_class,count(*) as cnt
from city_population
GROUP BY pop_class

最新文章

  1. AngularJS + Java---前台网页与后台数据库传递数据 基本结构
  2. mongodb学习04 操作详解(2)
  3. Linux Ubuntu常用终端命令
  4. Ubuntu升级没有声音的解决方法
  5. erlang 线上分析工具集锦
  6. 自己动手,丰衣足食!一大波各式各样的ImageView来袭!
  7. Java GC专家系列4:Apache的MaxClients设置及其对Tomcat Full GC的影响
  8. CSS 定位元素之 relative
  9. HDU 5534 Partial Tree
  10. 动态rem与1px边框问题的理解
  11. 本周新学的 GUI绘图技术
  12. A项目轶事之加入项目2个月
  13. requirejs官网
  14. sql语句实例练习
  15. Flask--templates-多个模板文件,视图函数如何判断查询路径
  16. 自动调整linux系统时间和时区与Internet时间同步
  17. SoapUI5.1.2安装和破解教程
  18. 1001.A+B Format (20)解题描述
  19. 170405、java版MD5工具类
  20. 设计模式之&mdash;&mdash;迭代器模式

热门文章

  1. java 创建 HMAC 签名
  2. BZOJ 1198 [HNOI2006]军机调度:dfs
  3. RQNOJ 169 最小乘车费用:水dp
  4. legend2---开发日志15(功能需求明确,设计好类和结构的好处是)
  5. html5--1.8超链接下
  6. POJ 2503 Babelfish(map,字典树,快排+二分,hash)
  7. L87
  8. C++中的宏和const
  9. android自定义控件(一) 官方文档的翻译
  10. BZOJ4836: [Lydsy1704月赛]二元运算