there is a strange behavior. I know that when using GROUP BY, all columns from SELECT should either be in group by clause, or have aggregate function. However, the next code shows something controversial:
// id is a primary key
select id, name from user group by name; <-- error because of group by (expected)
select id, name from user group by id; <-- works (unexpected!)
What am I missing? Can not find documentation that says about some potential special case for primary key in group by.
Expressions that are not encapsulated within an aggregate function and must be included in the GROUP BY Clause at the end of the SQL statement. This is an aggregate function such as the SUM, COUNT, MIN, MAX, or AVG functions.
The groupby clause is used to group the data according to particular column or row. 2. Having can be used without groupby clause,in aggregate function,in that case it behaves like where clause. groupby can be used without having clause with the select statement.
The PostgreSQL GROUP BY clause is used in collaboration with the SELECT statement to group together those rows in a table that have identical data. This is done to eliminate redundancy in the output and/or compute aggregates that apply to these groups.
An aggregate function allows you to perform a calculation on a set of values to return a single scalar value. We often use aggregate functions with the GROUP BY and HAVING clauses of the SELECT statement.
This is covered, but not especially obvious, in the docs:
When GROUP BY is present, or any aggregate functions are present, it is not valid for the SELECT list expressions to refer to ungrouped columns except within aggregate functions or when the ungrouped column is functionally dependent on the grouped columns, since there would otherwise be more than one possible value to return for an ungrouped column. A functional dependency exists if the grouped columns (or a subset thereof) are the primary key of the table containing the ungrouped column.
In this case, I'm guessing that id
is the primary key of the table user
which would make name
functionally dependent on id
.
Your primary key can guaranty unicity for the id
column.
I tried your statement on PostgreSQL 11.6 and 12.1 and I got the same error
SELECT id, name from unicity group by id;
ERROR: column "unicity.name" must appear in the GROUP BY clause or be used in an aggregate function
LINE 1: SELECT id, name from unicity group by id;
Which version of PostgreSQL are you using?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With