作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我相当精通 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/
我是一名优秀的程序员,十分优秀!