Skip site navigation (1) Skip section navigation (2)

Re: SQL query not working when GROUP BY / HAVING is used

From: Stephan Szabo <sszabo(at)megazone23(dot)bigpanda(dot)com>
To: juerg(dot)rietmann(at)pup(dot)ch
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: SQL query not working when GROUP BY / HAVING is used
Date: 2000-12-19 16:29:51
Message-ID: Pine.BSF.4.21.0012190826240.76591-100000@megazone23.bigpanda.com (view raw or flat)
Thread:
Lists: pgsql-sql
On Tue, 19 Dec 2000 juerg(dot)rietmann(at)pup(dot)ch wrote:

> Hello there
> 
> I have a question regarding a SQL statement.
> 
> When I execute (and that's what I need)
> 
> SELECT Zylinder.Z_durch_soll, Zylinder.Z_id, Zylinder.Z_durch_ist,
> ((Zylinder.Z_durch_soll+0.12) - Zylinder.Z_durch_ist) AS durchmesserdelta,
> (Zylinder.Z_durch_soll+0.12) AS effektiv
> FROM Auftrag,Zylinder_Typen, Zylinder
> WHERE Auftrag.A_nr = '11'
> AND (  Auftrag.A_farbe_1  > '0' AND Zylinder_Typen.Z_farbe='1' AND
> Zylinder_Typen.Z_SW='0')
> AND Zylinder_Typen.Z_durch_soll = Zylinder.Z_durch_soll
> AND Auftrag.A_Ztyp=Zylinder.Z_typ
> AND Z_A_nr = NULL
> AND Z_status = 'zcu'
> GROUP BY Zylinder.Z_durch_soll
> HAVING durchmesserdelta >= 0.085
> ORDER BY Zylinder_Typen.Z_durch_soll desc

This query is not legal SQL.  All columns in the select list of
a group by query must either be grouped columns or set value
functions (pretty much anyway).  The general construct is legal
but there are syntax rules for GROUP BY that you are violating.

If Z_id and Z_durch_ist are unique for values of Z_durch_soll
you might try grouping on all of them.  If not, the query above
is indeterminate since you are not specifying which Z_id and
which Z_durch_ist to use for a particular Z_durc_soll value.



In response to

pgsql-sql by date

Next:From: Josh BerkusDate: 2000-12-19 16:43:41
Subject: Re: Tree structure table normalization problem (do I need a trigger?)
Previous:From: Emils KlotinsDate: 2000-12-19 16:10:39
Subject: group by: properly?

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group