MySQL GROUP BY年龄范围包括空范围

时间:2010-07-14 15:17:01

标签: mysql

我正在尝试按年龄范围计算人数,我几乎可以解决2个问题:

  1. 如果在给定年龄范围内没有人(NULL),则该年龄范围不会出现在结果中。例如,在我的数据中没有“超过80”的条目,因此不会出现日期范围。基本上,当缺少日期范围时,编程中的错误似乎是错误的。

  2. 我想以特定的方式订购结果。在下面的查询中,因为ORDER BY是age_range,'20 - 29'的结果出现在'20岁以下'的结果之前。

  3. 以下是db表“queries”的示例:

    inquiry_id  birth_date
    1           1960-02-01
    2           1962-03-04
    3           1970-03-08
    4           1980-03-02
    5           1990-02-08
    

    以下是查询:

    SELECT
        CASE
            WHEN age < 20 THEN 'Under 20'
            WHEN age BETWEEN 20 and 29 THEN '20 - 29'
            WHEN age BETWEEN 30 and 39 THEN '30 - 39'
            WHEN age BETWEEN 40 and 49 THEN '40 - 49'
            WHEN age BETWEEN 50 and 59 THEN '50 - 59'
            WHEN age BETWEEN 60 and 69 THEN '60 - 69'
            WHEN age BETWEEN 70 and 79 THEN '70 - 79'
            WHEN age >= 80 THEN 'Over 80'
            WHEN age IS NULL THEN 'Not Filled In (NULL)'
        END as age_range,
        COUNT(*) AS count
    
        FROM (SELECT TIMESTAMPDIFF(YEAR, birth_date, CURDATE()) AS age FROM inquiries) as derived
    
        GROUP BY age_range
    
        ORDER BY age_range
    

    根据Wrikken的建议,这是一个简单的解决方案:

    SELECT
        SUM(IF(age < 20,1,0)) as 'Under 20',
        SUM(IF(age BETWEEN 20 and 29,1,0)) as '20 - 29',
        SUM(IF(age BETWEEN 30 and 39,1,0)) as '30 - 39',
        SUM(IF(age BETWEEN 40 and 49,1,0)) as '40 - 49',
        SUM(IF(age BETWEEN 50 and 59,1,0)) as '50 - 59',
        SUM(IF(age BETWEEN 60 and 69,1,0)) as '60 - 69',
        SUM(IF(age BETWEEN 70 and 79,1,0)) as '70 - 79',
        SUM(IF(age >=80, 1, 0)) as 'Over 80',
        SUM(IF(age IS NULL, 1, 0)) as 'Not Filled In (NULL)'
    
    FROM (SELECT TIMESTAMPDIFF(YEAR, birth_date, CURDATE()) AS age FROM inquiries) as derived
    

3 个答案:

答案 0 :(得分:12)

范围表(具有我的偏好)的替代方案,单行答案可以是:

SELECT
    SUM(IF(age < 20,1,0)) as 'Under 20',
    SUM(IF(age BETWEEN 20 and 29,1,0)) as '20 - 29',
    SUM(IF(age BETWEEN 30 and 39,1,0)) as '30 - 39',
    SUM(IF(age BETWEEN 40 and 49,1,0)) as '40 - 49',
...etc.
FROM inquiries;

答案 1 :(得分:3)

对结果进行排序的一种方法是在select语句中引入一个列,并为其提供一个等级值,表示您希望结果与其余部分一起排序,然后按该行排序,例如

SELECT
CASE
    WHEN age < 20 THEN 'Under 20'
    WHEN age BETWEEN 20 and 29 THEN '20 - 29'
    WHEN age BETWEEN 30 and 39 THEN '30 - 39'
    WHEN age BETWEEN 40 and 49 THEN '40 - 49'
    WHEN age BETWEEN 50 and 59 THEN '50 - 59'
    WHEN age BETWEEN 60 and 69 THEN '60 - 69'
    WHEN age BETWEEN 70 and 79 THEN '70 - 79'
    WHEN age >= 80 THEN 'Over 80'
    WHEN age IS NULL THEN 'Not Filled In (NULL)'
END as age_range,
COUNT(*) AS count,
 CASE
    WHEN age < 20 THEN 1
    WHEN age BETWEEN 20 and 29 THEN 2
    WHEN age BETWEEN 30 and 39 THEN 3
    WHEN age BETWEEN 40 and 49 THEN 4
    WHEN age BETWEEN 50 and 59 THEN 5
    WHEN age BETWEEN 60 and 69 THEN 6
    WHEN age BETWEEN 70 and 79 THEN 7
    WHEN age >= 80 THEN 8
    WHEN age IS NULL THEN 9
END as ordinal

FROM (SELECT TIMESTAMPDIFF(YEAR, birth_date, CURDATE()) AS age FROM inquiries) as derived

GROUP BY age_range

ORDER BY ordinal

答案 2 :(得分:1)

  1. 创建一个包含所有范围的表,并使用外连接。
  2. 在该表的另一列中按数值排序

    SELECT范围,.... FROM范围 LEFT JOIN(您的子查询)ON(ranges.range = your_range) ... ORDER BY range.year ASC