IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

Query to extract record from a table


IBM Mainframe Forums -> DB2
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
mubs55

New User


Joined: 19 Apr 2006
Posts: 12

PostPosted: Thu Dec 28, 2006 12:38 pm
Reply with quote

Hi,

I have got to select records from a table based on col B below it should have either 'AC' or 'TM' but if both 'AC' and 'TM' are present for same value in 'A' then it should select only the first record.
table
Code:
A    B
1    AC
1    TM
1    CA
2    AC
2    TM
3    AC
4    TM

output:
Code:
A    B
1    AC
2    AC
3    AC
4    TM

let me know how to write the query please.

Regards,
Mubeena
Back to top
View user's profile Send private message
raghunathns

Active User


Joined: 08 Dec 2005
Posts: 127
Location: rochester

PostPosted: Fri Dec 29, 2006 12:17 am
Reply with quote

try like this.

Code:
select * from tbl1 x
where 0 = (select count(*) from tbl1 y
           where y.a = x.a and y.b < x.b)
order by x.a, x.b

..
Back to top
View user's profile Send private message
raghunathns

Active User


Joined: 08 Dec 2005
Posts: 127
Location: rochester

PostPosted: Fri Dec 29, 2006 12:28 am
Reply with quote

you can try this way also.

Code:
select * from tbl1 x
where x.b = (select min(b) from tbl1 y where y.a = x.a)
order by x.a, x.b

..
change min to max to get maximum b value rows.
.
Back to top
View user's profile Send private message
priyesh.agrawal

Senior Member


Joined: 28 Mar 2005
Posts: 1448
Location: Chicago, IL

PostPosted: Mon Jan 01, 2007 7:09 am
Reply with quote

Mubeena... Did you get answer from above queries, of what you were looking for ?

Check it what I think on this...
Code:
SELECT * FROM (SELECT DISTINCT A, B FROM TABLE) WHERE B IN ('AC','TM');

Inner query should return the records with only first row among duplicates, outer query would filter out to keep only the one having "AC" or "TM".
I could not test it before posting, but a little variation of this concept should provide you the result- in case this doesnt work.
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic View Bookmarks
All times are GMT + 6 Hours
Forum Index -> DB2

 


Similar Topics
Topic Forum Replies
No new posts Extract the file name from another fi... DFSORT/ICETOOL 6
No new posts How to split large record length file... DFSORT/ICETOOL 10
No new posts Load new table with Old unload - DB2 DB2 6
No new posts SFTP Issue - destination file record ... All Other Mainframe Topics 2
No new posts FINDREP - Only first record from give... DFSORT/ICETOOL 3
Search our Forums:

Back to Top