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 72E525E46D9 for ; Fri, 20 Aug 2004 14:25:33 -0300 (ADT) 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 08426-01 for ; Fri, 20 Aug 2004 17:25:30 +0000 (GMT) Received: from 10.hostcorporate.com (unknown [69.41.231.186]) by svr1.postgresql.org (Postfix) with SMTP id 847795E46C1 for ; Fri, 20 Aug 2004 14:25:29 -0300 (ADT) Received: (qmail 4021 invoked from network); 20 Aug 2004 17:16:45 -0000 Received: from localhost (HELO localhost.localdomain) (127.0.0.1) by localhost.10.hostcorporate.com with SMTP; 20 Aug 2004 17:16:45 -0000 Message-ID: <4126348F.2090604@zara.6.isreserved.com> Date: Sat, 21 Aug 2004 00:27:43 +0700 From: David Garamond User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7b) Gecko/20040421 X-Accept-Language: en-us, en MIME-Version: 1.0 To: Bruno Wolff III Cc: pgsql-sql@postgresql.org Subject: Re: olympics ranking query References: <41262968.2000507@zara.6.isreserved.com> <20040820170148.GB26674@wolff.to> In-Reply-To: <20040820170148.GB26674@wolff.to> Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.0 tagged_above=0.0 required=5.0 tests= X-Spam-Level: X-Archive-Number: 200408/232 X-Sequence-Number: 18729 Bruno Wolff III wrote: > On Fri, Aug 20, 2004 at 23:40:08 +0700, > David Garamond wrote: > >>Challenge question: is there a simpler way to do query #1 (without any >>PL, and if possible without sequences too? > > You could use a subselect to count how many countries had a lower > medal ranking and add 1 to get the rank. This should be a lot more standard > than using sequences. It will probably be a little slower, but for tables > of that size it shouldn't be a big deal. Thanks for the tip. This is what I came up with: select (select count(*) from countrymedal c1 where c1.gold>c2.gold or (c1.gold=c2.gold and (c1.silver>c2.silver or (c1.silver=c2.silver and c1.bronze>c2.bronze))))+1 as rank, count(*) as numranker, gold, silver, bronze from countrymedal c2 group by gold, silver, bronze order by rank; -- dave