X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.2]) by svr1.postgresql.org (Postfix) with ESMTP id 132EED1F098 for ; Tue, 4 May 2004 12:13:06 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (neptune.hub.org [200.46.204.2]) (amavisd-new, port 10024) with ESMTP id 05950-03 for ; Tue, 4 May 2004 12:12:44 -0300 (ADT) Received: from p15124592.pureserver.info (p15124592.pureserver.info [217.160.181.164]) by svr1.postgresql.org (Postfix) with ESMTP id 06DEFD1F078 for ; Tue, 4 May 2004 12:12:44 -0300 (ADT) Received: from localhost (localhost [127.0.0.1]) by p15124592.pureserver.info (Postfix) with ESMTP id F2BB81795A for ; Tue, 4 May 2004 17:12:42 +0200 (CEST) Received: from sarge (p508A9FF5.dip0.t-ipconnect.de [80.138.159.245]) by p15124592.pureserver.info (Postfix) with ESMTP id 103BB17959 for ; Tue, 4 May 2004 17:12:42 +0200 (CEST) Received: from mk-os.de (unknown [192.168.0.210]) by sarge (Postfix) with ESMTP id E73633EC05 for ; Tue, 4 May 2004 17:12:40 +0200 (CEST) Message-ID: <4097B34B.7070102@mk-os.de> Date: Tue, 04 May 2004 17:14:19 +0200 From: Martin Knipper User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; de-AT; rv:1.6) Gecko/20040113 X-Accept-Language: de-at, de, en-us, en MIME-Version: 1.0 To: pgsql-sql@postgresql.org Subject: Re: Rank References: <003e01c42fa9$5677b240$4f00a8c0@MIDDINKS> <20040504101119.58d355cb.gry@ll.mit.edu> In-Reply-To: <20040504101119.58d355cb.gry@ll.mit.edu> X-Enigmail-Version: 0.83.5.0 X-Enigmail-Supports: pgp-inline, pgp-mime Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Virus-Scanned: by AMaViS at MK-OS X-Virus-Scanned: by amavisd-new at postgresql.org X-Spam-Status: No, hits=0.2 tagged_above=0.0 required=5.0 tests=RCVD_IN_NJABL, RCVD_IN_SORBS X-Spam-Level: X-Archive-Number: 200405/38 X-Sequence-Number: 17752 Am 04.05.2004 16:11 schrieb george young: > On Sun, 2 May 2004 02:22:37 +0800 > "Muhyiddin A.M Hayat" threw this fish to the penguins: > >>I Have below table >> >> id | site_name | point >>----+-----------+------- >> 1 | Site A | 40 >> 2 | Site B | 90 >> 3 | Site D | 22 >> 4 | Site X | 98 >> >> Would like to calc that Rank for each site, and look like >> >> id | site_name | point | rank >>----+-----------+-------+------ >> 1 | Site A | 40 | 3 >> 2 | Site B | 90 | 2 >> 3 | Site D | 22 | 4 >> 4 | Site X | 98 | 1 > > > Well, a simple minded solution would be: > > select id,site_name,point,(select count(*)from mytable t2 > where t2.point >= t1.point) as rank from mytable t1; > > id | site_name | point | rank > ----+-----------+-------+------ > 4 | Site X | 98 | 1 > 2 | Site B | 90 | 2 > 1 | Site A | 40 | 3 > 3 | Site D | 22 | 4 > (4 rows) > > If mytable is huge this may be prohibitively slow, but it's worth a try. > There's probably a self join that would be faster. Hmm... in fact: > > select t1.id,t1.site_name,t1.point,count(t2.point) as rank from mytable t1,mytable t2 > where t2.point >=t1.point group by t1.id,t1.site_name,t1.point; > > id | site_name | point | rank > ----+-----------+-------+------ > 3 | Site D | 22 | 4 > 2 | Site B | 90 | 2 > 4 | Site X | 98 | 1 > 1 | Site A | 40 | 3 > (4 rows) > > > -- George Young Another possibilty is to use a sequence: demo=# create temporary sequence ranking; demo=# select *,nextval('ranking') as rank from yourTable order by site_name asc; Greetins, Martin -- Martin Knipper www : http://www.mk-os.de Mail : knipper@mk-os.de Random Signature: ----------------- while( !asleep() ) sheep++;