Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PostgreSQL -must appear in the GROUP BY clause or be used in an aggregate function

I am getting this error in the pg production mode, but its working fine in sqlite3 development mode.

 ActiveRecord::StatementInvalid in ManagementController#index  PG::Error: ERROR:  column "estates.id" must appear in the GROUP BY clause or be used in an aggregate function LINE 1: SELECT "estates".* FROM "estates"  WHERE "estates"."Mgmt" = ...                ^ : SELECT "estates".* FROM "estates"  WHERE "estates"."Mgmt" = 'Mazzey' GROUP BY user_id  @myestate = Estate.where(:Mgmt => current_user.Company).group(:user_id).all 
like image 398
Hrishikesh Sardar Avatar asked Aug 05 '13 14:08

Hrishikesh Sardar


People also ask

Can GROUP BY be used with aggregate functions?

The GROUP BY statement is often used with aggregate functions ( COUNT() , MAX() , MIN() , SUM() , AVG() ) to group the result-set by one or more columns.

Can we use aggregate function without GROUP BY clause?

While all aggregate functions could be used without the GROUP BY clause, the whole point is to use the GROUP BY clause. That clause serves as the place where you'll define the condition on how to create a group. When the group is created, you'll calculate aggregated values.

Which clause can be used with aggregate functions?

An aggregate function can be used in a WHERE clause only if that clause is part of a subquery of a HAVING clause and the column name specified in the expression is a correlated reference to a group. If the expression includes more than one column name, each column name must be a correlated reference to the same group.

Is it possible to apply an aggregate function for each group in postgresql?

For each group, you can apply an aggregate function e.g., SUM() to calculate the sum of items or COUNT() to get the number of items in the groups. In this syntax: First, select the columns that you want to group e.g., column1 and column2 , and column that you want to apply an aggregate function ( column3 ).


1 Answers

If user_id is the PRIMARY KEY then you need to upgrade PostgreSQL; newer versions will correctly handle grouping by the primary key.

If user_id is neither unique nor the primary key for the 'estates' relation in question, then this query doesn't make much sense, since PostgreSQL has no way to know which value to return for each column of estates where multiple rows share the same user_id. You must use an aggregate function that expresses what you want, like min, max, avg, string_agg, array_agg, etc or add the column(s) of interest to the GROUP BY.

Alternately you can rephrase the query to use DISTINCT ON and an ORDER BY if you really do want to pick a somewhat arbitrary row, though I really doubt it's possible to express that via ActiveRecord.

Some databases - including SQLite and MySQL - will just pick an arbitrary row. This is considered incorrect and unsafe by the PostgreSQL team, so PostgreSQL follows the SQL standard and considers such queries to be errors.

If you have:

col1    col2 fred    42 bob     9 fred    44 fred    99 

and you do:

SELECT col1, col2 FROM mytable GROUP BY col1; 

then it's obvious that you should get the row:

bob     9 

but what about the result for fred? There is no single correct answer to pick, so the database will refuse to execute such unsafe queries. If you wanted the greatest col2 for any col1 you'd use the max aggregate:

SELECT col1, max(col2) AS max_col2 FROM mytable GROUP BY col1; 
like image 133
Craig Ringer Avatar answered Sep 19 '22 22:09

Craig Ringer