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 74293B43175 for ; Mon, 7 Jul 2003 14:33:09 +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 95379-09 for ; Mon, 7 Jul 2003 11:32:58 -0300 (ADT) Received: from linux012.intern.cityweb.de (linux012.intern.cityweb.de [195.71.215.252]) by svr1.postgresql.org (Postfix) with ESMTP id 19E7AB43170 for ; Mon, 7 Jul 2003 11:32:57 -0300 (ADT) Received: from zahedi.intern.cityweb.de (zahedi.intern.cityweb.de [193.189.226.198]) by linux012.intern.cityweb.de (8.12.8/8.12.8) with ESMTP id h67EWxPG021675 for ; Mon, 7 Jul 2003 16:33:00 +0200 Content-Type: text/plain; charset="us-ascii" From: Andreas Schmitz Reply-To: a.schmitz@cityweb.de Organization: Cityweb Network To: pgsql-sql@postgresql.org Subject: sort for ranking Date: Mon, 7 Jul 2003 16:14:14 +0200 User-Agent: KMail/1.4.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-Id: <200307071614.14913.a.schmitz@cityweb.de> X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200307/64 X-Sequence-Number: 14178 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