Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
Database Servers
DB2InformixIngresMS SQLOraclePervasive.SQLPostgreSQLProgressSybase
Desktop Databases
FileMakerFoxProMS AccessParadox
General
General DB TopicsDatabase Theory
Related Topics
Java Development.NET DevelopmentVB DevelopmentMore Topics ...

Database Forum / DB2 Topics / April 2007

Tip: Looking for answers? Try searching our database.

Selecting one arbitrarily chosen row from each group

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Ralf - 25 Apr 2007 08:12 GMT
Hi,

i try to find a query for selecting one arbitrarily chosen row from
each group of rows.

Example:

$ db2 "select * from T"

A           B           C
----------- ----------- -----------
       100           1          10
       100          20           2
       101           3           3

 3 record(s) selected.

The result should be 'Select A From T group by A' but with one (B,C)-
pair for each group.

So {(100, 1, 10), (101, 3, 3)} and {(100, 20, 2), (101, 3, 3)} would
both be valid results,
but not {(100, 1, 2), (101, 3, 3)}.

Is there a way to formulate this in SQL?

Thanks,
Ralf
Knut Stolze - 25 Apr 2007 09:28 GMT
> Hi,
>
[quoted text clipped - 21 lines]
>
> Is there a way to formulate this in SQL?

SELECT x.a, x.b, x.c
FROM   ( SELECT t.*, ROW_NUMBER() OVER(PARTITION BY t.a) AS rn
        FROM   yourTable AS t ) AS x
WHERE  x.rn = 1

A           B           C
----------- ----------- -----------
       100           1          10
       101           3           3

 2 record(s) selected.

Signature

Knut Stolze
DB2 z/OS Utilities Development
IBM Germany

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.