pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Quartile (etc) ranking in a SQL statement?
2+ messages / 2 participants
[nested] [flat]

* Quartile (etc) ranking in a SQL statement?
@ 2002-11-07 18:16  Jeff Boes <jboes@nexcerpt.com>
  0 siblings, 1 reply; 2+ messages in thread

From: Jeff Boes @ 2002-11-07 18:16 UTC (permalink / raw)
  To: pgsql-sql

Here's a puzzler:

Given a query that returns rows ranked by some criteria, how can I write
another query around it that will give me the (say) first quartile (top
25%)?  Another way of putting it is: if I have rows that look like this:


aaa | 1251
aba | 1197
cax | 1042
... | ...
axq |  23
(142 rows)

How can I write a query that will return these as

  1 | aaa | 1251
  2 | aba | 1197
  3 | cax | 1042
... | ... | ...
142 | axq |  23

-- 
Jeff Boes                                      vox 616.226.9550 ext 24
Database Engineer                                     fax 616.349.9076
Nexcerpt, Inc.                                 http://www.nexcerpt.com
           ...Nexcerpt... Extend your Expertise



^ permalink  raw  reply  [nested|flat] 2+ messages in thread

* Re: Quartile (etc) ranking in a SQL statement?
@ 2002-11-07 20:50  Jean-Luc Lachance <jllachan@nsd.ca>
  parent: Jeff Boes <jboes@nexcerpt.com>
  0 siblings, 0 replies; 2+ messages in thread

From: Jean-Luc Lachance @ 2002-11-07 20:50 UTC (permalink / raw)
  To: Jeff Boes <jboes@nexcerpt.com>; +Cc: pgsql-sql

In a PLPGPSQL script, once you know count(*) try 

execute ''select * from table limit '' || int4( theCount / 4);

If you want numbering, create a sequence and add nextval() to the query.

JLL



Jeff Boes wrote:
> 
> Here's a puzzler:
> 
> Given a query that returns rows ranked by some criteria, how can I write
> another query around it that will give me the (say) first quartile (top
> 25%)?  Another way of putting it is: if I have rows that look like this:
> 
> aaa | 1251
> aba | 1197
> cax | 1042
> ... | ...
> axq |  23
> (142 rows)
> 
> How can I write a query that will return these as
> 
>   1 | aaa | 1251
>   2 | aba | 1197
>   3 | cax | 1042
> ... | ... | ...
> 142 | axq |  23
> 
> --
> Jeff Boes                                      vox 616.226.9550 ext 24
> Database Engineer                                     fax 616.349.9076
> Nexcerpt, Inc.                                 http://www.nexcerpt.com
>            ...Nexcerpt... Extend your Expertise
> 
> ---------------------------(end of broadcast)---------------------------
> TIP 3: if posting/reading through Usenet, please send an appropriate
> subscribe-nomail command to majordomo@postgresql.org so that your
> message can get through to the mailing list cleanly



^ permalink  raw  reply  [nested|flat] 2+ messages in thread


end of thread, other threads:[~2002-11-07 20:50 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2002-11-07 18:16 Quartile (etc) ranking in a SQL statement? Jeff Boes <jboes@nexcerpt.com>
2002-11-07 20:50 ` Jean-Luc Lachance <jllachan@nsd.ca>

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