auto group by

From: Markus Bertheau <twanger(at)bluetwanger(dot)de>
To: postgres sql list <pgsql-sql(at)postgresql(dot)org>
Subject: auto group by
Date: 2002-01-24 18:00:45
Message-ID: 1011895246.29182.45.camel@entwicklung01.cenes.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Hello again,

here's another question:

suppose there's a table personen:

personen_id int primary key,
data text

and a table orders:

order_id int primary key,
personen_id int,
data text

. Now I want to know how many orders each person has, who has at least
one order. I would use the following SQL query:

select personen.personen_id, personen.text, count(order_id) from
personen join orders on personen.personen_id = orders.personen_id group
by personen.personen_id, personen.text

Suppose you had some more data in personen, like 10 extra fields, and
you want them to be included in the result. You would have to mention
each of them in the group by clause, not only say the personen_id. But
this information that is given to the db server seems a bit redundant to
me. If the personen_id is the same (which qualifies these rows for one
group), every single other field is the same also. Why isn't it
automagically included in the group by clause? You cannot do anything
useful with them but to group by them.

select [1] from tbl1 join tbl2 on tbl1.i=tbl2.i

Like: every row from tbl1 that is mentioned as is (that is not used in
an aggregate function) in the [1] marked location is automagically
included in the group by clause.

Why?

Enlightenment appreciated.

Markus Bertheau

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Tom Lane 2002-01-24 18:24:22 Re: why can a named subselect not be used in a where condition?
Previous Message Josh Berkus 2002-01-24 17:58:56 Re: why can a named subselect not be used in a where