B
B
Bogdan2019-12-04 16:30:35
PostgreSQL
Bogdan, 2019-12-04 16:30:35

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 fields
select * 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

Please tell me, is it possible to do something so that the subquery does not list all the fields, but only the id field . Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Melkij, 2019-12-04
@bogdan_uman

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 question

Ask a Question

731 491 924 answers to any question