前言
最近忙着给公司造数据,运用到了以前没接触到的oracle,贼多报错,搞心态,现在分享以下年龄段统计的语句
语法
sql对一张表实现不同进行多次统计,一般需要用到case,when,then这三个方法
实例
select case
when age<18 then '18岁以下'
when age between 18 and 35 then '18到35岁'
when age between 36 and 65 then '36到65岁'
when age>65 then '65岁以上' end as 年龄段,
count(*) as 人数
from (select floor(MONTHS_BETWEEN(sysdate,字段名)/12) as age from 表名 where BIRTHDATE is not null)
group by case
when age<18 then '18岁以下'
when age between 18 and 35 then '18到35岁'
when age between 36 and 65 then '36到65岁'
when age>65 then '65岁以上'
end;
因为我们的数据库表没有年龄段这个字段,所以先使用 floor(MONTHS_BETWEEN(sysdate,字段名)/12这个语句得到各个用户的年龄段,实际情况实际调整咯,如果有年龄这个字段那就更好了
运行之后得到下面的数据