Re: SQL challenge--top 10 for each key value? - Mailing list pgsql-sql

From Rod Taylor
Subject Re: SQL challenge--top 10 for each key value?
Date
Msg-id 1081481466.56361.808.camel@jester
Whole thread Raw
In response to Re: SQL challenge--top 10 for each key value?  (Greg Stark <gsstark@mit.edu>)
Responses Re: SQL challenge--top 10 for each key value?  (Tom Lane <tgl@sss.pgh.pa.us>)
Re: SQL challenge--top 10 for each key value?  (Josh Berkus <josh@agliodbs.com>)
List pgsql-sql
On Thu, 2004-04-08 at 19:33, Greg Stark wrote:
> Jeff Boes <jboes@nexcerpt.com> writes:
> 
> > I headed off in the direction of groups of SELECTs and UNIONs, and quit when I
> > got to something like four levels of "SELECT ... AS FOO" ...
> 
> four? wimp, that's nothing!
> 
> ok, seriously I think there's no way to do this directly with straight SQL.
> You would have to define a non-immutable function that has some temporary
> storage where it keeps track of how many it has seen. 

I don't believe that is true, though it is certainly is in PostgreSQL.

The spec has the ability to apply a progressive aggregate on the results
of a query (window function). Meaning you can accomplish things like
counting (ROW_NUMBER) or running totals.

Something along the lines of the below would accomplish what you want
according to spec. ROW_NUMBER() is a spec defined function. (6.10 of
SQL200N)
       SELECT *          FROM (SELECT ROW_NUMBER() OVER (DISTINCT query) AS counter                 <rest of query>
        )         WHERE counter > 10;
 



pgsql-sql by date:

Previous
From: Greg Stark
Date:
Subject: Re: SQL challenge--top 10 for each key value?
Next
From: Tom Lane
Date:
Subject: Re: SQL challenge--top 10 for each key value?