PostgreSQL聚合函数的filter子句
一张表存储了学生id,科目,分数三个字段,求每个学生60分以下与参加的总科目占比。(今天电脑不好用,图片总是这样) ? 其实一个count(*) filter? 就可以查出来,但是没用过PG的一个人竟然说我这么简单的SQL查询都不会,只是因为我没有用嵌套查询。回来总结了以下,自己想到了以下几种方法(只查询出了对应的数目没有做除法): ? --filter函数 select stu_id,count(*),count(*) filter (where score<60) as "不及格" from sherry.agg_filter group by stu_id ? --使用case when select stu_id,sum(case when score<60 then 1 else 0) as "不及格" from sherry.agg_filter group by stu_id ? --关联查询 select stu_id,(select count(*) as c from sherry.agg_filter b where a.stu_id = b.stu_id and b.score < 60) as t2 from sherry.agg_filter a group by a.stu_id ? --临时表关联 with raw as( select stu_id,count(*) as c1 from sherry.agg_filter where score < 60 group by stu_id ), raw2 as( select stu_id,count(*) as c2 from sherry.agg_filter? group by stu_id) select b.stu_id,c1,c2 from raw a right join raw2 b on a.stu_id = b.stu_id (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- 使用AJAX要注意引号
- .net – XML省略号/其他语法 – 它叫什么?
- c# – 实体框架代码第一个错误“定位服务器/实例指定的错误
- ruby-on-rails-4 – Rails 4服务器失败,Ruby 2.0分段错误,A
- 两种高性能I/O设计模式(Reactor/Proactor)的比较
- 从0开始学习Swift开发IOS应用(2)――为app添加图标
- postgresql – 在Postgres 9.2上增加work_mem和shared_buff
- org.xml.sax.SAXException: SimpleDeserializer encountere
- 使用node+vue.js实现SPA应用
- Cocos2d-x 3.x游戏开发之旅