sql - PostgreSQL GROUP BY 列必须出现在 GROUP BY
问题描述
SELECT
COUNT(follow."FK_accountId"),
score.*
FROM
(
SELECT items.*, AVG(reviews.score) as "averageScore" FROM "ITEM_VARIATION" as items
INNER JOIN "ITEM_REVIEW" as reviews ON reviews."FK_itemId"=items.id
GROUP BY items.id
) as score
INNER JOIN "ITEM_FOLLOWER" as follow ON score.id=follow."FK_itemId"
GROUP BY score.id
Inner Block 自己工作,我相信我遵循相同的格式。但是它输出错误:
ERROR: column "score.name" must appear in the GROUP BY clause or be used in an aggregate function
LINE 18: score.*
^
仅列出分数字段中的所有列吗?有超过 10 列要列出,所以如果不是唯一的,我想避免使用该解决方案
解决方案
未包含在聚合中的列必须在期间指定group by
SELECT
COUNT(follow."FK_accountId"),
score.id,
score.name
FROM
(
SELECT items.id as id, items.name as name, AVG(reviews.score) as "averageScore" FROM "ITEM_VARIATION" as items
INNER JOIN "ITEM_REVIEW" as reviews ON reviews."FK_itemId"=items.id
GROUP BY items.id, items.name
) as score
INNER JOIN "ITEM_FOLLOWER" as follow ON score.id=follow."FK_itemId"
GROUP BY score.id, score.name
推荐阅读
- r - 尝试使用 tidymodels 预测二进制变量等于 1 的概率
- javascript - 用百分比数字更新进度条?
- c - 在非常简单的 C windows api 程序上找不到错误 126 模块
- django - 在 Django 中发送电子邮件的正确文件结构
- c# - 使用 UI 自动化框架为 WPF 网格中的数据项设置自动化 ID 以实现自动化
- elasticsearch - elasticsearch 使用 Group by 保存的搜索
- c++ - 应用 `std::transform()` 函数后矢量元素出现意外行为
- python - Python selenium 单击按钮
- django - Django ORM中的双重反向过滤
- c# - 连接到 .Net Core 控制台应用程序中的现有 SOAP API 服务