Received: from localhost (unknown [200.46.204.191]) by postgresql.org (Postfix) with ESMTP id D12739F9A07 for ; Sun, 23 Sep 2007 19:46:06 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.191]) (amavisd-maia, port 10024) with ESMTP id 65413-10 for ; Sun, 23 Sep 2007 19:46:03 -0300 (ADT) X-Greylist: delayed 00:19:14.837921 by SQLgrey-1.7.5 Received: from mta02.xtra.co.nz (mta02.xtra.co.nz [210.54.141.253]) by postgresql.org (Postfix) with ESMTP id 6CECD9F9949 for ; Sun, 23 Sep 2007 19:46:02 -0300 (ADT) Received: from fep05.xtra.co.nz ([172.23.12.51]) by mta01.xtra.co.nz with ESMTP id <20070923222643.YXTA3767.mta01.xtra.co.nz@fep05.xtra.co.nz> for ; Mon, 24 Sep 2007 10:26:43 +1200 Received: from x1000.msqr.us ([125.238.106.4]) by fep05.xtra.co.nz with ESMTP id <20070923222643.IVQG7219.fep05.xtra.co.nz@x1000.msqr.us> for ; Mon, 24 Sep 2007 10:26:43 +1200 Received: from x1000.msqr.us (x1000.msqr.us [127.0.0.1]) by x1000.msqr.us (8.13.1/8.13.1) with ESMTP id l8NMQgF3023493 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO) for ; Mon, 24 Sep 2007 10:26:43 +1200 Received: (from apache@localhost) by x1000.msqr.us (8.13.1/8.13.1/Submit) id l8NMQaWY023490; Mon, 24 Sep 2007 10:26:36 +1200 Received: from 192.168.1.108 (SquirrelMail authenticated user matt) by msqr.us with HTTP; Mon, 24 Sep 2007 10:26:36 +1200 (NZST) Message-ID: <49480.192.168.1.108.1190586396.squirrel@msqr.us> Date: Mon, 24 Sep 2007 10:26:36 +1200 (NZST) Subject: How pull From: "Matt Magoffin" To: pgsql-sql@postgresql.org User-Agent: SquirrelMail/1.4.8-4.0.1.el4 MIME-Version: 1.0 Content-Type: text/plain;charset=iso-8859-1 Content-Transfer-Encoding: 8bit X-Priority: 3 (Normal) Importance: Normal X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.001 tagged_above=0 required=5 tests=BAYES_50=0.001 X-Spam-Level: X-Archive-Number: 200709/146 X-Sequence-Number: 29359 Hello, My SQL skills are limited and I'm struggling with a query where I want to return a single item of an aggregate join. The query looks like this: select (case when agg.avg_rating is null then 0.0 when agg.avg_rating < 0.75 then 0.5 when agg.avg_rating < 1.25 then 1.0 when agg.avg_rating < 1.75 then 1.5 when agg.avg_rating < 2.25 then 2.0 when agg.avg_rating < 2.75 then 2.5 when agg.avg_rating < 3.25 then 3.0 when agg.avg_rating < 3.75 then 3.5 when agg.avg_rating < 4.25 then 4.0 when agg.avg_rating < 4.75 then 4.5 else 5.0 end) as avg_rating, count(item.itemid) as item_count from media_item item inner join ( select rating.mediaitem_userrating_hjid as ritemid, avg(rating.rating) as avg_rating from media_item_rating rating, media_item item where rating.mediaitem_userrating_hjid = item.itemid group by rating.mediaitem_userrating_hjid ) as agg on item.itemid = agg.ritemid group by avg_rating order by avg_rating desc and a sample of results is this: avg_rating | item_count ------------+------------ 5.0 | 21 4.0 | 33 3.0 | 13 2.0 | 4 1.0 | 1 What I want as well is the ID of the item (and possibly it's avg_rating value) from the "agg" join with the highest avg_rating for each output row... something like this avg_rating | item_count | item_id | item_rating ------------+----------------------------------- 5.0 | 21 | 109890 | 4.9 4.0 | 33 | 89201 | 4.1 3.0 | 13 | 119029 | 2.8 2.0 | 4 | 182999 | 2.2 1.0 | 1 | 1929 | 1.0 So the intention in this example is that item #109890 has an average rating of 4.9 and that is the highest rating within the > 4.75 rating group. If anyone had any tips I'd greatly appreciate it. -- m@