zoukankan      html  css  js  c++  java
  • 用sql语句实现年龄分段统计

    SELECT
        CASE
    WHEN (age >= 10 AND age <= 20) THEN
        '10-20'
    WHEN (age >= 21 AND age <= 30) THEN
        '21-30'
    ELSE
        '30-'
    END 'eag_layer',
     count(*) emps
    FROM
        address_book
    GROUP BY
        CASE
    WHEN (age >= 10 AND age <= 20) THEN
        '10-20'
    WHEN (age >= 21 AND age <= 30) THEN
        '21-30'
    ELSE
        '30-'
    END
    ORDER BY
        1;
    SELECT '10-20' 年龄段, COUNT(*) 人数
    FROM [Table]
    WHERE [年龄] BETWEEN 10 AND 20
    UNION ALL
    SELECT '21-30' 年龄段, COUNT(*) 人数
    FROM [Table]
    WHERE [年龄] BETWEEN 21 AND 30
    UNION ALL
    SELECT '31' 年龄段, COUNT(*) 人数
    FROM [Table]
    WHERE [年龄] > 30 
    select case when [年龄] BETWEEN 10 AND 20  then '10-20'
                when [年龄] BETWEEN 20 AND 30  then '20-30'
                when [年龄] > 30 then '30以上' end as '年龄段',
           count(*) as '人数' FROM [Table] 

    先将年龄除10取整

    select floor(年龄/10) as age from 表
    • 1

    再根据年龄整数分组统计

    select age ,count(age) from
    (
    select floor(年龄/10) as age from 表
    )
    group by age

    这样基本效果就出来了,达到楼主的要求就要加如函数计算了

    sql语法

    select convert(varchar,age*10)+'--'+convert(varchar,(age+1)*10) ,count(age) from
    (
    select floor(年龄/10) as age from 表
    )
    group by age

    oracle语法

    select age*10 || '--'|| (age+1)*10 ,count(age) from
    (
    select floor(年龄/10) as age from 表
    )
    group by age
  • 相关阅读:
    利用GitHub+Node.js+Hexo搭建个人博客(一)
    更丰富的符号工具包 Font Awesome
    Markdwon入门2
    Codechef:Fibonacci Number/FN——求通项+二次剩余+bsgs
    二次剩余理论
    基姆拉尔森公式和蔡勒公式(计算星期几)
    幂方程(模意义下)
    etcd
    mysql group by
    UUID
  • 原文地址:https://www.cnblogs.com/telwanggs/p/9248092.html
Copyright © 2011-2022 走看看