MVSFORUMS.com Forum Index MVSFORUMS.com
A Community of and for MVS Professionals
 
 FAQFAQ   SearchSearch   Quick Manuals   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Query to control the rows for each key

 
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Database
View previous topic :: View next topic  
Author Message
rublu
Beginner


Joined: 14 Jun 2005
Posts: 5
Topics: 2

PostPosted: Tue Jun 14, 2005 5:29 pm    Post subject: Query to control the rows for each key Reply with quote

Hi all,

I have a table T1. In that I have 2 fields ID and VALUE.

T1
---
ID VALUE
***********

For each ID there are about 1000 values present.

Now my question was, I want a query which will select 2 values for each ID, though there present more than 2 records for that particular ID.

i.e. the out put should be like

T1
---
ID - VALUE
************
1 - a
1 - b
2 - a
2 - c
3 - f
3 - g

Is it possible to control the rows for each key...?
_________________
------------
Ruben
Back to top
View user's profile Send private message
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12380
Topics: 75
Location: San Jose

PostPosted: Tue Jun 14, 2005 6:30 pm    Post subject: Reply with quote

rublu,

Try this sql. I did not get a chance to test it

Code:

SELECT C.ID                                 
      ,C.VAL                               
  FROM (SELECT A.ID       ID               
              ,A.VALUE    VAL             
              ,COUNT(*)   SEQ             
           FROM TABLE A                       
               ,TABLE B                       
          WHERE A.ID     = B.ID           
            AND A.VALUE <= B.VALUE   
          GROUP BY A.ID                 
                  ,A.VALUE) C           
  WHERE C.SEQ <= 2                         
          ;                                 



Hope this helps...

Cheers

Kolusu
_________________
Kolusu
www.linkedin.com/in/kolusu
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Database All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


MVSFORUMS
Powered by phpBB © 2001, 2005 phpBB Group