gpt4 book ai didi

postgresql - 正确的 GROUP BY 语法

转载 作者:行者123 更新时间:2023-11-29 12:31:43 30 4
gpt4 key购买 nike

我相当精通 mySQL 和 MSSQL,但我才刚刚开始使用 postgres。我确定这是一个简单的问题,所以要简短:

SQL error:

ERROR: column "incidents.open_date" must appear in the GROUP BY clause or be used in an aggregate function

In statement:
SELECT date(open_date), COUNT(*)
FROM incidents
GROUP BY 1
ORDER BY open_date

open_date 的类型是timestamp with time zone,如果我使用GROUP BY date(open_date),我会得到相同的结果。

我已经尝试查看 postgres 文档和一些在线示例,但一切似乎都表明这应该是有效的。

最佳答案

问题出在 ORDER BY 子句中未经修饰的 open_date

应该这样做:

  SELECT date(open_date), COUNT(*)
FROM incidents
GROUP BY date(open_date)
ORDER BY date(open_date);

这也可行(尽管出于维护原因我不喜欢使用整数来引用列):

  SELECT date(open_date), COUNT(*)
FROM incidents
GROUP BY 1
ORDER BY 1;

关于postgresql - 正确的 GROUP BY 语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9317345/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com