为什么它给我 GROUP BY Description_lenght_buckets 作为无效的列名称? [重复]

问题描述 投票:0回答:1
SELECT
CASE
WHEN LEN(b.app_desc) < 500 THEN 'Short'
WHEN LEN(b.app_desc) BETWEEN 500 AND 1000 THEN 'Medium'
ELSE 'Long'
END AS Description_lenght_buckets, AVG(a.user_rating) AS Avg_rating
FROM AppleStore AS a
JOIN appleStore_description AS b
ON a.id = b.id
GROUP BY Description_lenght_buckets
ORDER BY Avg_rating

我尝试的唯一方法是使用 ALTER 创建新列,然后使用 GROUP BY,然后它就会起作用。

这段代码工作的唯一方法是使用 ALTER 吗?

sql sqlite group-by
1个回答
0
投票

您可以通过子选择来完成:

SELECT Description_lenght_buckets, AVG(user_rating) AS Avg_rating
FROM (
    SELECT
    CASE
        WHEN LENGTH(b.app_desc) < 500 THEN 'Short'
        WHEN LENGTH(b.app_desc) BETWEEN 500 AND 1000 THEN 'Medium'
        ELSE 'Long'
    END AS Description_lenght_buckets, user_rating
    FROM AppleStore AS a
    JOIN appleStore_description AS b
    ON a.id = b.id
) t
GROUP BY Description_lenght_buckets
ORDER BY AVG(user_rating);

说明:您的子选择选择您想要聚合的各个记录。

小提琴:http://sqlfiddle.com/#!5/79ba4/1

© www.soinside.com 2019 - 2024. All rights reserved.