Answer the question
In order to leave comments, you need to log in
GROUP BY and field enumeration?
Hello. Tell me please.
If you make a query directly to the table, then you do not need to list the fields in GROUP BY , only the id field is enough.
But when you do this to a subquery, then you already need to list all the fieldsselect * from "Statistics" group by id;
with st as (select * from "Statistics")
select * from st group by id;
SQL Error [42803]: ERROR: column "st.deviceId" must appear in the GROUP BY clause or be used in an aggregate function
Position: 92
Answer the question
In order to leave comments, you need to log in
This is functional dependency. If the planner can reduce your subqueries or cte to a unique index, functional dependency will allow you to limit yourself to specifying only the fields of this unique index.
Counter question: why do you need such a grouping at all? For the given requests it is useless and only harmful.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question