From: Herouth Maoz <herouth@oumail.openu.ac.il>
To: Giampiero Raschetti <Giampiero.Raschetti@popso.it>
To: pgsql-sql@postgreSQL.org
Subject: Re: [SQL] PgSQL 6.5.1 and Group BY pb
Date: Wed, 25 Aug 1999 15:58:29 +0300
Message-ID: <l03130304b3e996ea9e0d@[147.233.159.109]> (raw)
In-Reply-To: <37C3CEB9.AB63E3DE@popso.it>
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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-sql@postgresql.org
Cc: herouth@oumail.openu.ac.il, Giampiero.Raschetti@popso.it
Subject: Re: [SQL] PgSQL 6.5.1 and Group BY pb
In-Reply-To: <l03130304b3e996ea9e0d@[147.233.159.109]>
References: <37C3CEB9.AB63E3DE@popso.it> <l03130304b3e996ea9e0d@[147.233.159.109]>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox