Re: [SQL] PgSQL 6.5.1 and Group BY pb

From: Herouth Maoz <herouth(at)oumail(dot)openu(dot)ac(dot)il>
To: Giampiero Raschetti <Giampiero(dot)Raschetti(at)popso(dot)it>, pgsql-sql(at)postgreSQL(dot)org
Subject: Re: [SQL] PgSQL 6.5.1 and Group BY pb
Date: 1999-08-25 12:58:29
Message-ID: l03130304b3e996ea9e0d@[147.233.159.109]
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

At 14:08 +0300 on 25/08/1999, Giampiero Raschetti wrote:

> And now the output query with GROUP BY:
>
> template1=> SELECT g.nome,u.uid,u.id FROM gruppi g, usergroup u GROUP BY
> g.nome;
> ERROR: Illegal use of aggregates or non-group column in target list
> template1=> SELECT nome,id FROM gruppi GROUP BY nome;
> ERROR: Illegal use of aggregates or non-group column in target list
>
> And now the output query with SELECT DISTINCT:
>
> template1=> SELECT DISTINCT ON g.nome g.nome,u.uid,u.id from gruppi g,
> usergroup u;
> ERROR: parser: parse error at or near "."

I think you have a misunderstanding about the purpose of group by statements.

In fact, the above seems to indicate that you are not well aware of what
joins are, or at least you don't know that you have to restrict them to
make sense of your data. You really have to add WHERE g.id = u.id.

Anyway, what was it that you wanted to display in that second query of
yours? For each nome, display some id that matches it? Define "some". Which
id do you want. DON'T use "distinct on". It makes no sense. SQL is defined
in such a way that the returned set of results will be the same in all
implementations that respect the standard. If you use "DISTINCT ON", it
will pick up a representative at will, and it may not be the same
representative picked by other implementations. So... it is not a standard
part of SQL.

In order to be more standard, you have to tell it how to pick the
representative. For example, tell him to give you the smallest-number id
that matches a group. This is done with min():

SELECT g.nome, min(u.uid), min(u.id)
FROM gruppi g, usergroup u
WHERE g.id = u.id
GROUP BY g.nome;

This will work. The question is whether this is what you wanted.

Herouth

--
Herouth Maoz, Internet developer.
Open University of Israel - Telem project
http://telem.openu.ac.il/~herutma

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Howie 1999-08-25 13:29:56 Re: [SQL] Dumping Oracle tables into Postgres
Previous Message Giampiero Raschetti 1999-08-25 11:08:41 PgSQL 6.5.1 and Group BY pb