pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Rod Taylor <pg@rbt.ca>
To: Greg Stark <gsstark@mit.edu>
Cc: Jeff Boes <jboes@nexcerpt.com>
Cc: pgsql-sql@postgresql.org
Subject: Re: SQL challenge--top 10 for each key value?
Date: Thu, 08 Apr 2004 23:31:07 -0400
Message-ID: <1081481466.56361.808.camel@jester> (raw)
In-Reply-To: <87r7uyf3ki.fsf@stark.xeocode.com>
References: <33af4fa4e4ec230574cefcff737ea40a@news.teranews.com>
	<87r7uyf3ki.fsf@stark.xeocode.com>

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;




view thread (16+ messages)  latest in thread

Message-ID: <1081481466.56361.808.camel@jester>
Permalink:  ../1081481466.56361.808.camel@jester/
Also on:    postgresql.org/message-id/1081481466.56361.808.camel@jester

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: pg@rbt.ca, gsstark@mit.edu, jboes@nexcerpt.com
  Subject: Re: SQL challenge--top 10 for each key value?
  In-Reply-To: <1081481466.56361.808.camel@jester>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox