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 B04975E37CE for ; Fri, 20 Aug 2004 14:21:13 -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 06748-02 for ; Fri, 20 Aug 2004 17:21:09 +0000 (GMT) Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by svr1.postgresql.org (Postfix) with ESMTP id C48C95E3639 for ; Fri, 20 Aug 2004 14:21:08 -0300 (ADT) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.12.11/8.12.11) with ESMTP id i7KHL8Fh028622; Fri, 20 Aug 2004 13:21:08 -0400 (EDT) To: David Garamond Cc: pgsql-sql@postgresql.org Subject: Re: olympics ranking query In-reply-to: <412630C8.5000004@zara.6.isreserved.com> References: <41262968.2000507@zara.6.isreserved.com> <22979.1093020382@sss.pgh.pa.us> <412630C8.5000004@zara.6.isreserved.com> Comments: In-reply-to David Garamond message dated "Sat, 21 Aug 2004 00:11:36 +0700" Date: Fri, 20 Aug 2004 13:21:08 -0400 Message-ID: <28621.1093022468@sss.pgh.pa.us> From: Tom Lane 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/231 X-Sequence-Number: 18728 David Garamond writes: > This is not quite the same. The ranks are sequential, but they skip, so > as to match the number of participating countries. Oh, I missed that bit. What you really want here is a "running sum" function, that is SELECT running_sum(numranker) as rank, * FROM (same subselect as before) ss; There is no such thing in standard SQL, because it's fundamentally dependent on the assumption of the input data coming in a particular order, which is Evil Incarnate according to the relational worldview. But it's not too hard to do in PLs that allow persistent state. I recall Elein having exhibited one in plpython(?) not too long ago --- you might find it on techdocs or varlena.com. You could brute-force it with a subselect (essentially "SUM(numranker) over all rows that should precede this one") but that would involve recomputing the whole subselect for each output row, which doesn't seem very attractive. regards, tom lane