As a general rule, any column not listed in the GROUP BY
clause should show up aggregated in the SELECT
list.
For example s.name
should show up as max(s.name)
or min(s.name)
since it's not present n the GROUP BY
list. However, PostgreSQL implements functional dependency (a SQL Standard feature) for the GROUP BY
clause, and detects that s.name
is dependent in the s.id
column (that is probably a PK); in short, there's a single possible value s.name
for each s.id
. Therefore, there's no need in PostgreSQL to aggregate this column (you can, but it's not needed).
On the flip side, for lookupStudyType.description
PostgreSQL cannot determine if it's functionally dependent on s.id
or not. You'll need to aggregate it as max(lookupStudyType.description)
or min(lookupStudyType.description)
, or any other aggregation expression.
As a side note, I have rarely seen functional dependency implemented in other databases. Isn't PostgreSQL awesome? (I'm not affiliated with PostgreSQL in any way).
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…