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 / February 2005

Tip: Looking for answers? Try searching our database.

how to extract one row from each range data

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
janet - 22 Feb 2005 18:44 GMT
hi

I want to extract one row from each range data.

For example , we had table had following data

name    age    department
janet   22     HR
John    45     IT
Jane    25     IT
Tom     35     HR
Nancy   33     Sales

I want to get any one row from age range 20~30, 30~40, 40~50

is there a single way to do that?
Tonkuma - 22 Feb 2005 23:26 GMT
------------------------------ Commands Entered
------------------------------
SELECT name, age, department
 FROM (SELECT q.*
            , ROWNUMBER() OVER(PARTITION BY age/10 ORDER BY age) rno
         FROM RANGE_TEST q
      ) r
WHERE rno = 1
ORDER BY age;
------------------------------------------------------------------------------

NAME    AGE    DEPARTMENT
------- ------ ----------
janet       22 HR
Nancy       33 Sales
John        45 IT        

 3 record(s) selected.
Tonkuma - 23 Feb 2005 00:08 GMT
------------------------------ Commands Entered
------------------------------
SELECT L, U, name, age, department
 FROM (SELECT q.*, L, U
            , ROWNUMBER() OVER(PARTITION BY L ORDER BY age) rno
         FROM RANGE_TEST q
            , (VALUES (20,29), (30,39), (40,49)) Range(L, U)
        WHERE age BETWEEN L AND U
      ) r
WHERE rno = 1
ORDER BY age;
------------------------------------------------------------------------------

L           U           NAME    AGE    DEPARTMENT
----------- ----------- ------- ------ ----------
        20          29 janet       22 HR
        30          39 Nancy       33 Sales
        40          49 John        45 IT

 3 record(s) selected.
janet - 25 Feb 2005 03:21 GMT
hi, Tonkuma

Thanks very much for quickly response! Both answered my question. The
second way is more flexible...

> ------------------------------ Commands Entered
> ------------------------------
[quoted text clipped - 16 lines]
>
>   3 record(s) selected.
 
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



©2008 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.