按年龄分组和计数

Posted

技术标签:

【中文标题】按年龄分组和计数【英文标题】:Group and count by age breaks 【发布时间】:2016-05-05 11:48:07 【问题描述】:

对于航行(年份、船舶),如何在标准 SQL 或 MS-ACCESS 中按年龄间隔对水手进行分组和计数?

YEAR    SHIP            SAILOR_AGE
2003    Flying dolphin  33
2003    Flying dolphin  33
2003    Flying dolphin  34
2001    Flying dolphin  23
2003    Flying dolphin  35
2001    Flying dolphin  38
2001    Flying dolphin  31
2003    Flying dolphin  36
2003    Columbine       41
2003    Columbine       42
2003    Flying dolphin  27
2003    Flying dolphin  51
2003    Flying dolphin  46

我尝试了什么:

SELECT YEAR, SHIP, SAILOR_AGE,  COUNT (*) as `NUMBERS` 
FROM TABLE
GROUP BY YEARS, SHIP, SAILOR_AGE;

它给了我每年的水手人数:

例子:

YEAR |SHIP           |SAILOR_AGE  | NUMBERS
------------------------------------------
2003 | Flying dolphin| 33         |   2

如何按间隔对水手年龄分组

例子:

From 20th to 40th year's old  
From 40th  to 60th year's old 

【问题讨论】:

你为什么不用CASE..WHEN 【参考方案1】:

您可以尝试使用CASE 指定以下年龄范围:

SELECT YEARS, SHIP, AgeRange, COUNT (*) as `NUMBERS` 
FROM (SELECT YEAR
            , SHIP
            , CASE WHEN SAILOR_AGE BETWEEN 21 AND 40 THEN '21-40'
                   WHEN SAILOR_AGE BETWEEN 41 AND 60 THEN '41-60'
                   ELSE 'XX-XX' -- anything goes here
              END as AgeRange
      FROM YourTable
)x
GROUP BY YEARS, SHIP, AgeRange;

【讨论】:

【参考方案2】:

如果你想要列中的值,你可以使用条件聚合:

SELECT YEAR, SHIP, COUNT(*) as NUMSAILORS,
       SUM(CASE WHEN SAILOR_AGE BETWEEN 21 AND 40 THEN 1 ELSE 0 END) as AGE_21_40,
       SUM(CASE WHEN SAILOR_AGE BETWEEN 41 AND 60 THEN 1 ELSE 0 END) as AGE_41_60
FROM TABLE
GROUP BY YEARS, SHIP;

【讨论】:

【参考方案3】:

因为这个 - 给定标签 - 很可能是 Access SQL,你可以这样:

SELECT 
    YEAR, 
    SHIP, 
    INT(SAILOR_AGE / 20) * 20 AS AgeGroup,  
    COUNT(*) As [NUMBERS]
FROM 
    TABLE
GROUP BY 
    YEAR, 
    SHIP, 
    INT(SAILOR_AGE / 20) * 20

【讨论】:

效果很好,但我不太清楚 case when 语句! 也许,但是 Access SQL 不知道 Case-When 我正在尝试将我的 Access db 转换为 sqlite db,但我没有找到一个免费且快速工作的转换工具... 哦,那你可能会遇到麻烦。与 Access 相比,SQLite 受到严重限制,实际上在数据类型方面。 我需要将我的 Access Db 转换为使用 GIS qgis 的地理数据库! (sqlite -spatialite 扩展)【参考方案4】:

您可以使用CASE EXPRESSION 进行单个查询:

SELECT t.years,t.ship,
       CASE WHEN t.Sailor_age between 21 and 40 then 'From 20th to 40th'
            WHEN t.Sailor_age between 41 and 60 then 'From 40th to 60th'
            ELSE 'Other Ages'
       END as Range
FROM YourTable t
GROUP BY t.years,t.ship,CASE WHEN t.Sailor_age between 21 and 40 then 'From 20th to 40th' WHEN t.Sailor_age between 41 and 60 then 'From 40th to 60th' ELSE 'Other Ages' END
ORDER BY t.years,t.ship,Range

或者,如果您希望使用条件聚合将其作为单行:

SELECT t.years,t.ship,
       COUNT(CASE WHEN t.Sailor_age between 21 and 40 then 1 end) as Age_21_to_40,
       COUNT(CASE WHEN t.Sailor_age between 41 and 60 then 1 end) as Age_41_to_60,
FROM YourTable t
GROUP BY t.years,t.ship
ORDER BY t.years,t.ship

【讨论】:

【参考方案5】:

我想说实现这一目标的最佳方法是创建辅助表:

DECLARE @hlptbl TABLE(DescOfGrp VARCHAR(50), AgeFrom INT, AgeTo INT)
INSERT INTO @hlptbl (DescOfGrp, AgeFrom, AgeTo)
VALUES('20 to 40', 20, 40),
('41 to 60', 40, 60)

然后,你必须以这种方式加入数据:

SELECT t1.[Year], t1.SHIP, t2.DescOfGrp, COUNT(t1.SHIP) AS Number
FROM TableA As t1 INNER JOIN @hlptbl AS t2 ON 
           t1.SAILOR_AGE>=t2.AgeFrom AND t1.SAILOR_AGE <=t2.AgeTo
GROUP BY t1.[Year], t1.SHIP, t2.DescOfGrp

结果:

Year    SHIP    DescOfGrp   Number
2001    Flying dolphin  20 to 40    3
2003    Columbine       41 to 60    2
2003    Flying dolphin  20 to 40    6
2003    Flying dolphin  41 to 60    2

【讨论】:

以上是关于按年龄分组和计数的主要内容,如果未能解决你的问题,请参考以下文章

如何用spass给一组数据比如说年龄分组?

按条件分组和计数

Postgres 按年龄组确定前 10 个域(排名 + 分组依据)

SQL语句按年龄分组,统计各个年龄的人数

按日期分组和计数 (R)

SQL按间隔分组,计数和求和