X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (developer.postgresql.org [64.117.224.193]) by svr1.postgresql.org (Postfix) with ESMTP id B86BDB43B2F for ; Mon, 7 Jul 2003 20:58:06 +0000 (GMT) Received: from svr1.postgresql.org ([64.117.224.193]) by localhost (svr1.postgresql.org [64.117.224.193]) (amavisd-new, port 10024) with ESMTP id 05215-02 for ; Mon, 7 Jul 2003 17:57:53 -0300 (ADT) Received: from www.postgresql.com (www.postgresql.com [64.117.225.209]) by svr1.postgresql.org (Postfix) with ESMTP id B5D2BB439AF for ; Mon, 7 Jul 2003 17:57:42 -0300 (ADT) Received: from beamish.nsd.ca (beamish.nsd.ca [205.150.156.194]) by www.postgresql.com (Postfix) with ESMTP id 0530ACF732D for ; Mon, 7 Jul 2003 14:20:51 -0300 (ADT) Received: (from smap@localhost) by beamish.nsd.ca (8.9.3/8.9.3) id NAA18159; Mon, 7 Jul 2003 13:19:49 -0400 X-Authentication-Warning: beamish.nsd.ca: smap set sender to using -f Received: from reddog.nsd.ca(192.168.101.30) by beamish.nsd.ca via smap (V2.1/2.1+anti-relay+anti-spam) id xma018155; Mon, 7 Jul 03 13:19:20 -0400 Received: from nsd.ca (jllachan-linux.nsd.ca [192.168.101.148]) by reddog.nsd.ca (8.8.7/8.8.7) with ESMTP id NAA02677; Mon, 7 Jul 2003 13:10:06 -0400 Message-ID: <3F09AB9C.DA259352@nsd.ca> Date: Mon, 07 Jul 2003 13:19:24 -0400 From: Jean-Luc Lachance X-Mailer: Mozilla 4.8 [en] (X11; U; Linux 2.4.18-24.7.x i686) X-Accept-Language: en MIME-Version: 1.0 To: a.schmitz@cityweb.de Cc: pgsql-sql@postgresql.org Subject: Re: sort for ranking References: <200307071614.14913.a.schmitz@cityweb.de> Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200307/78 X-Sequence-Number: 14192 Andreas, try select sum_user,nextval('tipp_eval_seq')-1 as ranking from ( select user_sum from tbl_sums order by user_sum desc) as ss; JLL Andreas Schmitz wrote: > > Hello *, > > I have a little problem that confuses me. We are gathering values from a table > as a sum to insert them into another table. I also need to get a ranking at > insert (i.e. Highest points will get first place and so on). I tried ton > invole a sequence to qualify the ranking by select at insert. > > So I tried the following (smaller example) > > select setval('tipp_eval_seq',1); > select sum_user,nextval('tipp_eval_seq')-1 as ranking from tbl_sums order by > ranking desc, user_sum asc; > > user_sum | ranking > ----------+--------- > 46 | 30 > 45 | 26 > 44 | 28 > 43 | 25 > 42 | 1 > 41 | 2 > 39 | 3 > 38 | 27 > 36 | 19 > 35 | 18 > 34 | 20 > 31 | 24 > 30 | 17 > 29 | 15 > 28 | 16 > 27 | 12 > 26 | 11 > 25 | 23 > 24 | 21 > 23 | 10 > 19 | 13 > 16 | 9 > 12 | 7 > 11 | 8 > 10 | 29 > 8 | 6 > 7 | 5 > 6 | 14 > 2 | 4 > 1 | 22 > (30 rows) > > As you can see, the sums are sorted correctly but the ranking is a mess. I > recongnized that the select seems to follow primarily the internal table > order. Is there any way to solve this nicely. Hints and solutions are > appreciated. > > Thanks in advance > > -Andreas > > -- > Andreas Schmitz - Phone +49 201 8501 318 > Cityweb-Technik-Service-Gesellschaft mbH > Friedrichstr. 12 - Fax +49 201 8501 104 > 45128 Essen - email a.schmitz@cityweb.de > > ---------------------------(end of broadcast)--------------------------- > TIP 9: the planner will ignore your desire to choose an index scan if your > joining column's datatypes do not match