pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Which SQL query makes it possible to optain the 3 greatest values of an interger list ?
3+ messages / 3 participants
[nested] [flat]

* Which SQL query makes it possible to optain the 3 greatest values of an interger list ?
@ 2001-09-12 08:41  Yoann <yaubi@yaubi.com>
  0 siblings, 2 replies; 3+ messages in thread

From: yaubi@yaubi.com @ 2001-09-12 08:41 UTC (permalink / raw)
  To: pgsql-sql

OO( Sql Problem )Oo. 

That is to say a table of 5 inputs of 1 integer field : 

   Table = { (1); (12); (3); (9); (4) }

We want to obtain a result of 1 input of 3 fields, 
corresponding to the 3 greatest values of Table, 
by descending order : 

   Result = { (12; 9; 4) } 

=> Which SQL query makes it possible to obtain Result from Table ?
We certainly need to use sub-queries, but how ?

Thank you in advance for your help !

Yoann AUBINEAU



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

* Re: Which SQL query makes it possible to optain the 3 greatest values of an interger list ?
@ 2001-09-15 00:08  Wei Weng <wweng@kencast.com>
  parent: Yoann <yaubi@yaubi.com>
  1 sibling, 0 replies; 3+ messages in thread

From: Wei Weng @ 2001-09-15 00:08 UTC (permalink / raw)
  To: Yoann <yaubi@yaubi.com>; pgsql-sql

I would use

SELECT id FROM table ORDER BY id LIMIT 0, 3;

in order to get the top 3 results. The key is "Limit" keyword.



==========
Wei Weng
Network Software Engineer
KenCast Inc.
 


> -----Original Message-----
> From: pgsql-sql-owner@postgresql.org
> [mailto:pgsql-sql-owner@postgresql.org]On Behalf Of Yoann
> Sent: Wednesday, September 12, 2001 4:41 AM
> To: pgsql-sql@postgresql.org
> Subject: [SQL] Which SQL query makes it possible to optain the 3
> greatest values of an interger list ?
> 
> 
> OO( Sql Problem )Oo. 
> 
> That is to say a table of 5 inputs of 1 integer field : 
> 
>    Table = { (1); (12); (3); (9); (4) }
> 
> We want to obtain a result of 1 input of 3 fields, 
> corresponding to the 3 greatest values of Table, 
> by descending order : 
> 
>    Result = { (12; 9; 4) } 
> 
> => Which SQL query makes it possible to obtain Result from Table ?
> We certainly need to use sub-queries, but how ?
> 
> Thank you in advance for your help !
> 
> Yoann AUBINEAU
> 
> ---------------------------(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] 3+ messages in thread

* Re: Which SQL query makes it possible to optain the 3 greatest
@ 2001-09-15 00:11  Patrik Kudo <kudo@partitur.se>
  parent: Yoann <yaubi@yaubi.com>
  1 sibling, 0 replies; 3+ messages in thread

From: Patrik Kudo @ 2001-09-15 00:11 UTC (permalink / raw)
  To: Yoann <yaubi@yaubi.com>; +Cc: pgsql-sql

On 12 Sep 2001, Yoann wrote:

> OO( Sql Problem )Oo.
>
> That is to say a table of 5 inputs of 1 integer field :
>
>    Table = { (1); (12); (3); (9); (4) }

Am I right that what you have is this?

CREATE TABLE T (v integer);
INSERT INTO T (v) VALUES (1);
INSERT INTO T (v) VALUES (12);
INSERT INTO T (v) VALUES (3);
INSERT INTO T (v) VALUES (9);
INSERT INTO T (v) VALUES (4);

In that case you could do the following:

SELECT v FROM T ORDER BY v DESC LIMIT 3;

This will select the values, sort them in descending order and limit the
result to 3 rows.

I hope it helps.

Regards,
Patrik Kudo

> We want to obtain a result of 1 input of 3 fields,
> corresponding to the 3 greatest values of Table,
> by descending order :
>
>    Result = { (12; 9; 4) }
>
> => Which SQL query makes it possible to obtain Result from Table ?
> We certainly need to use sub-queries, but how ?
>
> Thank you in advance for your help !
>
> Yoann AUBINEAU




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


end of thread, other threads:[~2001-09-15 00:11 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2001-09-12 08:41 Which SQL query makes it possible to optain the 3 greatest values of an interger list ? Yoann <yaubi@yaubi.com>
2001-09-15 00:08 ` Wei Weng <wweng@kencast.com>
2001-09-15 00:11 ` Re: Which SQL query makes it possible to optain the 3 greatest Patrik Kudo <kudo@partitur.se>

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