X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id B115A3A3BB3 for ; Sun, 7 Nov 2004 18:32:17 +0000 (GMT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 52283-09 for ; Sun, 7 Nov 2004 18:32:08 +0000 (GMT) Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by svr1.postgresql.org (Postfix) with ESMTP id D73773A3C28 for ; Sun, 7 Nov 2004 18:32:08 +0000 (GMT) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.13.1/8.13.1) with ESMTP id iA7IVu9M022996; Sun, 7 Nov 2004 13:31:56 -0500 (EST) To: mailreg@numerixtechnology.de Cc: pgsql-sql Subject: Re: tricky GROUP BY / JOIN question In-reply-to: <418DF96C.4090501@numerixtechnology.de> References: <418DF96C.4090501@numerixtechnology.de> Comments: In-reply-to T E Schmitz message dated "Sun, 07 Nov 2004 10:31:08 +0000" Date: Sun, 07 Nov 2004 13:31:56 -0500 Message-ID: <22995.1099852316@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.3 tagged_above=0.0 required=5.0 tests=UPPERCASE_25_50 X-Spam-Level: X-Archive-Number: 200411/55 X-Sequence-Number: 19374 T E Schmitz writes: > This is *almost* what I need: > SELECT > BRAND.BRAND_NAME, > MODEL.MODEL_NAME, > min (ITEM.PRICE),max (ITEM.PRICE) > *min (CONDITION.POSITION),max (CONDITION.POSITION)* > FROM ITEM > left outer join MODEL on MODEL_PK =ITEM.MODEL_FK > left outer join BRAND on BRAND.BRAND_PK =MODEL.BRAND_FK > left outer join CONDITION on CONDITION.CONDITION_PK = ITEM.CONDITION_FK > group by BRAND.BRAND_NAME,MODEL.MODEL_NAME > In the result I don't want min/max(POSITION) but CONDITION.NAME of min > and max(POSITION) for each MODEL. I think you could do something like SELECT BRAND.BRAND_NAME, MODEL.MODEL_NAME, min (ITEM.PRICE),max (ITEM.PRICE) (select name from condition c1 where position = min(condition.position)), (select name from condition c2 where position = max(condition.position)), FROM ITEM left outer join MODEL on MODEL_PK =ITEM.MODEL_FK left outer join BRAND on BRAND.BRAND_PK =MODEL.BRAND_FK left outer join CONDITION on CONDITION.CONDITION_PK = ITEM.CONDITION_FK group by BRAND.BRAND_NAME,MODEL.MODEL_NAME ie do a sub-select to get the desired name. You need Postgres 7.4 or later to get this to work --- before that we would have mis-interpreted the aggregate calls to indicate aggregation within the sub-selects. The current interpretation is per SQL spec: since the aggregate argument is a variable of the outer select, the aggregation occurs with respect to that select, and the aggregate result is passed down to the sub-select as a scalar. regards, tom lane