简体   繁体   English

postgres-SELECT / GROUP BY中的部分列-列必须出现在GROUP BY子句中或在聚合函数中使用

[英]postgres - partial column in SELECT/GROUP BY - column must appear in the GROUP BY clause or be used in an aggregate function

Both the following two statements produce an error in Postgres: 以下两个语句均在Postgres中产生错误:

SELECT substring(start_time,1,8) AS date, count(*) as total from cdrs group by date;
SELECT substring(start_time,1,8) AS date, count(*) as total from cdrs group by substring(start_time,1,8);

The error is: 错误是:

column "cdrs.start_time" must appear in the GROUP BY clause or be used in an aggregate function 列“ cdrs.start_time”必须出现在GROUP BY子句中或在聚合函数中使用

My reading of postgres docs is that both SELECT and GROUP BY can use an expression postgres 8.3 SELECT 我对postgres文档的阅读是SELECT和GROUP BY都可以使用表达式 postgres 8.3 SELECT

The start_time field is a string and has a date/time in form ccyymmddHHMMSS. start_time字段是一个字符串,并具有格式为ccyymmddHHMMSS的日期/时间。 In mySQL they both produce desired and expected results: 在mySQL中,它们都产生预期的结果:

+----------+-------+
| date     | total |
+----------+-------+
| 20091028 |     9 |
| 20091029 |   110 |
| 20091120 |    14 |
| 20091121 |     4 |
+----------+-------+
4 rows in set (0.00 sec)

I need to stick with Postgres (heroku). 我需要坚持使用Postgres(heroku)。 Any suggestions? 有什么建议么?

ps there is lots of other discussion around that talks about missing items in GROUP BY and why mySQL accepts this, why others don't ... strict adherence to SQL spec etc etc, but I think this is sufficiently different to 1062158/converting-mysql-select-to-postgresql and 1769361/postgresql-group-by-different-from-mysql to warrant a separate question. ps周围还有很多其他讨论,涉及GROUP BY中的缺失项以及为什么mySQL接受这一点,为什么其他人不……严格遵守SQL规范等,但我认为这与1062158 / converting-完全不同mysql-select-to-postgresql1769361 / postgresql-group-by-from-mysql有区别 ,需要单独提出一个问题。

You did something else that you didn't describe in the question, as both of your queries work just fine. 您做了其他您未在问题中描述的事情,因为这两个查询都可以正常工作。 Tested on 8.5 and 8.3.8: 在8.5和8.3.8上测试:

# create table cdrs (start_time text);
CREATE TABLE

# insert into cdrs (start_time) values ('20090101121212'),('20090101131313'),('20090510040603');
INSERT 0 3

# SELECT substring(start_time,1,8) AS date, count(*) as total from cdrs group by date;
   date   | total
----------+-------
 20090510 |     1
 20090101 |     2
(2 rows)

# SELECT substring(start_time,1,8) AS date, count(*) as total from cdrs group by substring(start_time,1,8);
   date   | total
----------+-------
 20090510 |     1
 20090101 |     2
(2 rows)

Just to summarise, error 总结一下,错误

column "cdrs.start_time" must appear in the GROUP BY clause or be used in an aggregate function 列“ cdrs.start_time”必须出现在GROUP BY子句中或在聚合函数中使用

was caused (in this case) by ORDER BY start_time clause. (在这种情况下)是由ORDER BY start_time子句引起的。 Full statement needed to be either: 完整的陈述必须是:

SELECT substring(start_time,1,8) AS date, count(*) as total FROM cdrs GROUP BY substring(start_time,1,8) ORDER BY substring(start_time,1,8);

or 要么

    SELECT substring(start_time,1,8) AS date, count(*) as total FROM cdrs GROUP BY date ORDER BY date;

Two simple things you might try: 您可以尝试两种简单的方法:

  1. Upgrade to postgres 8.4.1 升级到Postgres 8.4.1
    Both queries Work Just Fine For Me (tm) under pg841 这两个查询在pg841下对我来说都很好

  2. Group by ordinal position 按顺序分组
    That is, GROUP BY 1 in this case. 也就是说,在这种情况下, GROUP BY 1

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Postgres列必须出现在GROUP BY子句中,或者用在聚合函数中 - Postgres column must appear in the GROUP BY clause or be used in an aggregate function Postgres:错误:列必须出现在GROUP BY子句中或在聚合函数中使用 - Postgres: ERROR: column must appear in the GROUP BY clause or be used in an aggregate function Postgres:列必须出现在GROUP BY子句中或在聚合函数中使用 - Postgres: column must appear in the GROUP BY clause or be used in an aggregate function Postgres SQL:列必须出现在 GROUP BY 子句中或用于聚合 function - Postgres SQL: column must appear in the GROUP BY clause or be used in an aggregate function “列必须出现在GROUP BY子句中或在聚合函数中使用” - “column must appear in the GROUP BY clause or be used in an aggregate function” TypeORM:列必须出现在 GROUP BY 子句中或用于聚合 function - TypeORM: column must appear in the GROUP BY clause or be used in an aggregate function 错误:列必须出现在 GROUP BY 子句中或用于聚合函数中 - ERROR: column must appear in the GROUP BY clause or be used in an aggregate function x列必须出现在GROUP BY子句中或在聚合函数中使用 - Column x must appear in the GROUP BY clause or be used in an aggregate function 错误:列必须出现在GROUP BY子句中或在聚合函数中使用 - ERROR: column must appear in the GROUP BY clause or be used in an aggregate function PostgreSQL列必须出现在GROUP BY子句中或在聚合函数中使用 - Postgresql column must appear in the GROUP BY clause or be used in an aggregate function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM