Re: olympics ranking query

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: David Garamond <lists(at)zara(dot)6(dot)isreserved(dot)com>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: olympics ranking query
Date: 2004-08-20 17:21:08
Message-ID: 28621.1093022468@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

David Garamond <lists(at)zara(dot)6(dot)isreserved(dot)com> 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

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message David Garamond 2004-08-20 17:27:43 Re: olympics ranking query
Previous Message David Garamond 2004-08-20 17:11:36 Re: olympics ranking query