按列计算并创建类别年龄显示总和

时间:2014-10-22 15:10:38

标签: mysql sum case multiple-columns

这是我的查询。 我计算年龄,在每个类别的总和发生后,一切都正确。

SELECT
        SUM(CASE WHEN AGE <18 THEN 1 ELSE 0 END) AS '<18', 
        SUM(CASE WHEN AGE >= 18 AND AGE <= 24 THEN 1 ELSE 0 END) AS '18-24', 
        SUM(CASE WHEN AGE >= 25 AND AGE <= 34 THEN 1 ELSE 0 END) AS '25-34',
        SUM(CASE WHEN AGE >= 35 AND AGE <= 44 THEN 1 ELSE 0 END) AS '35-44', 
        SUM(CASE WHEN AGE >= 45 AND AGE <= 54 THEN 1 ELSE 0 END) AS '45-54', 
        SUM(CASE WHEN AGE >= 55 THEN 1 ELSE 0 END)AS '>=55'
    FROM (SELECT t_personne.pers_date_naissance, t_personne.pers_date_inscription, 
            TIMESTAMPDIFF(Year, t_personne.pers_date_naissance, t_personne.pers_date_inscription) - 
            CASE WHEN MONTH(t_personne.pers_date_naissance) > MONTH(t_personne.pers_date_inscription) 
            OR (MONTH(t_personne.pers_date_naissance) = MONTH(t_personne.pers_date_inscription) 
            AND DAY(t_personne.pers_date_naissance) > DAY(t_personne.pers_date_inscription))
            THEN 1 ELSE 0 END AS AGE
            FROM t_personne) AS Total

给:

<18|18-24|25-34|35-44|45-54|>=55    
67 |693  |1619 |1486 |616  |93

但我喜欢这样:

AGE  |Total|
<18  |67   |
18-24|693  |
25-34|1619 |
35-44|1486 |
45-54|616  |
>=55 |93   |

2 个答案:

答案 0 :(得分:0)

您可以使用带有列名的交叉连接来转置它

SELECT 
    age_range,
    CASE age_range 
        WHEN '<18' THEN SUM(CASE WHEN AGE <18 THEN 1 ELSE 0 END)
        WHEN '18-24' THEN SUM(CASE WHEN AGE >= 18 AND AGE <= 24 THEN 1 ELSE 0 END)
        WHEN '25-34' THEN SUM(CASE WHEN AGE >= 25 AND AGE <= 34 THEN 1 ELSE 0 END)
        WHEN '35-44' THEN SUM(CASE WHEN AGE >= 35 AND AGE <= 44 THEN 1 ELSE 0 END)
        WHEN '45-54' THEN SUM(CASE WHEN AGE >= 45 AND AGE <= 54 THEN 1 ELSE 0 END)
        WHEN '>=55' THEN SUM(CASE WHEN AGE >= 55 THEN 1 ELSE 0 END)
    END value
FROM 
(   SELECT 
        t_personne.pers_date_naissance, 
        t_personne.pers_date_inscription, 
        TIMESTAMPDIFF(Year, t_personne.pers_date_naissance, t_personne.pers_date_inscription) 
        - CASE 
            WHEN MONTH(t_personne.pers_date_naissance) > MONTH(t_personne.pers_date_inscription) 
             OR (MONTH(t_personne.pers_date_naissance) = MONTH(t_personne.pers_date_inscription) 
             AND DAY(t_personne.pers_date_naissance) > DAY(t_personne.pers_date_inscription))
            THEN 1 ELSE 0 
          END AS AGE
    FROM t_personne
) AS Total
CROSS JOIN
(   SELECT '<18' age_range UNION ALL
    SELECT '18-24' UNION ALL
    SELECT '25-34' UNION ALL
    SELECT '35-44' UNION ALL
    SELECT '45-54' UNION ALL
    SELECT '>=55'
)a
GROUP BY age_range
ORDER BY FIELD(age_range, '<18', '18-24', '25-34', '35-44', '45-54', '>=55')

FIDDLE DEMO

答案 1 :(得分:-1)

不那么优雅,但它会起作用

SELECT
    '<18' as AGE,COUNT(AGE) AS Total FROM
    (SELECT t_personne.pers_date_naissance, t_personne.pers_date_inscription, 
        TIMESTAMPDIFF(Year, t_personne.pers_date_naissance, t_personne.pers_date_inscription) - 
        CASE WHEN MONTH(t_personne.pers_date_naissance) > MONTH(t_personne.pers_date_inscription) 
        OR (MONTH(t_personne.pers_date_naissance) = MONTH(t_personne.pers_date_inscription) 
        AND DAY(t_personne.pers_date_naissance) > DAY(t_personne.pers_date_inscription))
        THEN 1 ELSE 0 END AS AGE
        FROM t_personne) as Totals where AGE <18
UNION
 SELECT
    '18-24',COUNT(AGE) AS Total FROM
    (SELECT t_personne.pers_date_naissance, t_personne.pers_date_inscription, 
        TIMESTAMPDIFF(Year, t_personne.pers_date_naissance, t_personne.pers_date_inscription) - 
        CASE WHEN MONTH(t_personne.pers_date_naissance) > MONTH(t_personne.pers_date_inscription) 
        OR (MONTH(t_personne.pers_date_naissance) = MONTH(t_personne.pers_date_inscription) 
        AND DAY(t_personne.pers_date_naissance) > DAY(t_personne.pers_date_inscription))
        THEN 1 ELSE 0 END AS AGE
        FROM t_personne) as Totals where AGE >= 18 AND AGE <= 24 
 UNION
 SELECT
    '25-34',COUNT(AGE) AS Total FROM
    (SELECT t_personne.pers_date_naissance, t_personne.pers_date_inscription, 
        TIMESTAMPDIFF(Year, t_personne.pers_date_naissance, t_personne.pers_date_inscription) - 
        CASE WHEN MONTH(t_personne.pers_date_naissance) > MONTH(t_personne.pers_date_inscription) 
        OR (MONTH(t_personne.pers_date_naissance) = MONTH(t_personne.pers_date_inscription) 
        AND DAY(t_personne.pers_date_naissance) > DAY(t_personne.pers_date_inscription))
        THEN 1 ELSE 0 END AS AGE
        FROM t_personne) as Totals where AGE >= 25 AND AGE <= 34 
UNION
 SELECT
    '35-44',COUNT(AGE) AS Total FROM
    (SELECT t_personne.pers_date_naissance, t_personne.pers_date_inscription, 
        TIMESTAMPDIFF(Year, t_personne.pers_date_naissance, t_personne.pers_date_inscription) - 
        CASE WHEN MONTH(t_personne.pers_date_naissance) > MONTH(t_personne.pers_date_inscription) 
        OR (MONTH(t_personne.pers_date_naissance) = MONTH(t_personne.pers_date_inscription) 
        AND DAY(t_personne.pers_date_naissance) > DAY(t_personne.pers_date_inscription))
        THEN 1 ELSE 0 END AS AGE
        FROM t_personne) as Totals where AGE >= 35 AND AGE <= 44
UNION
 SELECT
    '45-54',COUNT(AGE) AS Total FROM
    (SELECT t_personne.pers_date_naissance, t_personne.pers_date_inscription, 
        TIMESTAMPDIFF(Year, t_personne.pers_date_naissance, t_personne.pers_date_inscription) - 
        CASE WHEN MONTH(t_personne.pers_date_naissance) > MONTH(t_personne.pers_date_inscription) 
        OR (MONTH(t_personne.pers_date_naissance) = MONTH(t_personne.pers_date_inscription) 
        AND DAY(t_personne.pers_date_naissance) > DAY(t_personne.pers_date_inscription))
        THEN 1 ELSE 0 END AS AGE
        FROM t_personne) as Totals where AGE >= 45 AND AGE <= 54
UNION
 SELECT
    '>=55',COUNT(AGE) AS Total FROM
    (SELECT t_personne.pers_date_naissance, t_personne.pers_date_inscription, 
        TIMESTAMPDIFF(Year, t_personne.pers_date_naissance, t_personne.pers_date_inscription) - 
        CASE WHEN MONTH(t_personne.pers_date_naissance) > MONTH(t_personne.pers_date_inscription) 
        OR (MONTH(t_personne.pers_date_naissance) = MONTH(t_personne.pers_date_inscription) 
        AND DAY(t_personne.pers_date_naissance) > DAY(t_personne.pers_date_inscription))
        THEN 1 ELSE 0 END AS AGE
        FROM t_personne) as Totals where AGE >= 55