mysql选择CASE没有检索其他列值?

时间:2013-06-27 05:39:14

标签: mysql sql

作为我从我的问题中得到的答案,由于某种原因,我坚决申请我的代码,为什么它没有用。

这是我桌上数据的一部分。

enter image description here

原始代码:

SELECT AGE_RANGE, COUNT(*) FROM (
    SELECT CASE
        WHEN YearsOld BETWEEN 0 AND 5 THEN '0-5'
        WHEN YearsOld BETWEEN 6 AND 10 THEN '6-10'
        WHEN YearsOld BETWEEN 11 AND 15 THEN '11-15'
        WHEN YearsOld BETWEEN 16 AND 20 THEN '16-20'
        WHEN YearsOld BETWEEN 21 AND 30 THEN '21-30'
        WHEN YearsOld BETWEEN 31 AND 40 THEN '31-40'
        WHEN YearsOld > 40 THEN '40+'
        END AS 'AGE_RANGE'
        FROM (
            SELECT YEAR(CURDATE())-YEAR(DATE(birthdate)) 'YearsOld' 
            FROM MyTable
        ) B
) A
GROUP BY AGE_RANGE

这是结果。

enter image description here

我正在尝试做的是,我正在尝试添加另一个专栏,该专栏会依赖于该区域中有多少人位置,就像您在图片上看到的那样在顶部,包括珀斯,西澳大利亚,森伯里,维多利亚等。

首先尝试修复我的问题

如下所示,我添加了位置和COUNT(位置)loc来获取位置的名称,并计算表中重复的位置数。

SELECT AGE_RANGE, COUNT(*), location, COUNT(location) loc FROM (
    SELECT CASE
        WHEN YearsOld BETWEEN 0 AND 5 THEN '0-5'
        WHEN YearsOld BETWEEN 6 AND 10 THEN '6-10'
        WHEN YearsOld BETWEEN 11 AND 15 THEN '11-15'
        WHEN YearsOld BETWEEN 16 AND 20 THEN '16-20'
        WHEN YearsOld BETWEEN 21 AND 30 THEN '21-30'
        WHEN YearsOld BETWEEN 31 AND 40 THEN '31-40'
        WHEN YearsOld > 40 THEN '40+'
        END AS 'AGE_RANGE', 'location'
        FROM (
            SELECT YEAR(CURDATE())-YEAR(DATE(birthday)) 'YearsOld' 
            FROM event_participants
        ) B
) A
FROM event_participants WHERE location <> '' GROUP BY location HAVING loc >= 1 ORDER BY loc DESC LIMIT 5

结果是......

enter image description here

显然,在上帝的帮助下听到的声音并不顺利。

我第二次尝试解决此问题

SELECT AGE_RANGE, COUNT(*), location FROM (
    SELECT CASE
        WHEN YearsOld BETWEEN 0 AND 5 THEN '0-5'
        WHEN YearsOld BETWEEN 6 AND 10 THEN '6-10'
        WHEN YearsOld BETWEEN 11 AND 15 THEN '11-15'
        WHEN YearsOld BETWEEN 16 AND 20 THEN '16-20'
        WHEN YearsOld BETWEEN 21 AND 30 THEN '21-30'
        WHEN YearsOld BETWEEN 31 AND 40 THEN '31-40'
        WHEN YearsOld > 40 THEN '40+'
        END AS 'AGE_RANGE', 'location'
        FROM (
            SELECT YEAR(CURDATE())-YEAR(DATE(birthday)) 'YearsOld' 
            FROM event_participants
        ) B
) A
GROUP BY AGE_RANGE

结果是......

enter image description here

代替显示国家/地区的名称,它只显示位置。

嗯..

长话短说,代码有什么问题?这个东西有什么解决方案吗?

就像,我希望的结果是,

AGE_RANGE COUNT(*) location            total number of same location

16-20        4     Name of location                 #

21-30       45     Name of location                 #

31-40       79     Name of location                 #

40+        102     Name of location                 #

3 个答案:

答案 0 :(得分:1)

这应该可行:将每个子查询视为自己的表,因此您只需确保选择所需的所有列。 (结束时为AS)实际上只是一个列值。您可能还希望在位置上分组,以便您可以获得不同位置的相同年龄范围。 (加拿大11-15岁,美国11-15岁)

SELECT AGE_RANGE, COUNT(*), A.location FROM (
        SELECT CASE
            WHEN YearsOld BETWEEN 0 AND 5 THEN '0-5'
            WHEN YearsOld BETWEEN 6 AND 10 THEN '6-10'
            WHEN YearsOld BETWEEN 11 AND 15 THEN '11-15'
            WHEN YearsOld BETWEEN 16 AND 20 THEN '16-20'
            WHEN YearsOld BETWEEN 21 AND 30 THEN '21-30'
            WHEN YearsOld BETWEEN 31 AND 40 THEN '31-40'
            WHEN YearsOld > 40 THEN '40+'
            END AS 'AGE_RANGE', B.location
            FROM (
                SELECT YEAR(CURDATE())-YEAR(DATE(birthday)) 'YearsOld',
                location /* << just missing this select */
                FROM event_participants
            ) B
    ) A
    GROUP BY A.location, AGE_RANGE

答案 1 :(得分:1)

试试这个

SELECT AGE_RANGE, COUNT(*), location FROM (
        SELECT CASE
            WHEN YearsOld BETWEEN 0 AND 5 THEN '0-5'
            WHEN YearsOld BETWEEN 6 AND 10 THEN '6-10'
            WHEN YearsOld BETWEEN 11 AND 15 THEN '11-15'
            WHEN YearsOld BETWEEN 16 AND 20 THEN '16-20'
            WHEN YearsOld BETWEEN 21 AND 30 THEN '21-30'
            WHEN YearsOld BETWEEN 31 AND 40 THEN '31-40'
            WHEN YearsOld > 40 THEN '40+'
            END AS 'AGE_RANGE', 'location'
            FROM (
                SELECT YEAR(CURDATE())-YEAR(DATE(birthday)) 'YearsOld'
                FROM event_participants
            ) B
    ) A
    GROUP BY location, AGE_RANGE

答案 2 :(得分:0)

这不正确: -

SELECT AGE_RANGE, COUNT(*), location FROM

当您使用GROUP BY时,select中的属性列表必须应用于聚合函数或必须是GROUP BY子句的一部分。

在下面的示例中,由于age-group中的人可能属于不同的位置,因此您无法显示该位置的名称: -

AGE_RANGE COUNT(*) location            total number of same location

16-20        4     Name of location                 #

21-30       45     Name of location                 #