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

Why it's giving me that GROUP BY Description_lenght_buckets as Invalid Column name? [duplicate]

提问人:mohannad mabrouk 提问时间:10/21/2023 最后编辑:Jensmohannad mabrouk 更新时间:10/21/2023 访问量:50

问:

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 分组

评论

0赞 Jens 10/21/2023
不能在 group by 子句中使用别名
0赞 Jonas Metzler 10/21/2023
请显示示例数据和预期结果
0赞 forpas 10/22/2023
您没有使用 SQLite: db-fiddle.com/f/qmzxRKKiULYiuXeCRgTXhx/0。你为什么不选择正确的标签?

答:

1赞 Lajos Arpad 10/21/2023 #1

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

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

评论

0赞 Lajos Arpad 10/21/2023
@mohannadmabrouk很乐意提供帮助。