一种范围统计和聚合的SQL
程序员文章站
2023-12-29 20:29:34
...
例子:
select sum(case when age<=6 then 1 else 0 END) as '0-6', sum(case when age>=7 and age<=18 then 1 else 0 END) as '7-18', sum(case when age>=19 and age<=25 then 1 else 0 END) as '19-25', sum(case when age>=26 and age<=30 then 1 else 0 END) as '26-30', sum(case when age>=31 and age<=35 then 1 else 0 END) as '31-35', sum(case when age>=36 and age<=40 then 1 else 0 END) as '36-40', sum(case when age>=41 and age<=45 then 1 else 0 END) as '41-45', sum(case when age>=46 and age<=50 then 1 else 0 END) as '46-50', sum(case when age>=51 and age<=55 then 1 else 0 END) as '51-55', sum(case when age>=56 and age<=60 then 1 else 0 END) as '56-60', sum(case when age>=61 and age<=65 then 1 else 0 END) as '61-65', sum(case when age>=66 and age<=70 then 1 else 0 END) as '66-70', sum(case when age>=71 and age<=75 then 1 else 0 END) as '71-75', sum(case when age>=76 and age<=80 then 1 else 0 END) as '76-80', sum(case when age>=81 then 1 else 0 END) as '≧81' from ( select age from his_sign hs ) age
推荐阅读
-
一种范围统计和聚合的SQL
-
sql server和oracle行转列的一种典型方法
-
sql查询出现次数最多的记录的名称和现次数以及QT聚合查找失败解决
-
最新统计排名前十的SQL和NoSQL数据库排行榜
-
使用SQL语句统计数据时sum和count函数中使用if判断条件的讲解
-
SQL分组函数group by和聚合函数(COUNT、MAX、MIN、AVG、SUM)的几点说明
-
SQL 同时显示总计、分组小计、和明细数据的统计查询 ,应用 SQL笛卡尔积 链接 、COUNT()函数 和 UNION
-
PHP查找一周内的数据 和一段范围的数据 如何写SQL
-
sql server 统计数据库中某个字段的值,剔除重复数据和查找重复
-
通过订单数据表生成每日的订单数量和增长量统计报表,该如何编写sql?