Portal | Manuals | References | Downloads | Info | Programs | JCLs | Mainframe wiki | Quick Ref
IBM Mainframe Computers Forums Index
 
Register
 
IBM Mainframe Computers Forums Index Mainframe: Search IBM Mainframe Forum: FAQ Memberlist Profile Log in to check your private messages Log in
 
Query to extract record from a table

 
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> DB2
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    Post subject: Query to extract record from a table
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    Post subject: Query to extract record
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    Post subject: Query to extract record
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: 1452
Location: Chicago, IL

PostPosted: Mon Jan 01, 2007 7:09 am    Post subject: Re: Query to extract record
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    IBMMAINFRAMES.com Support Forums -> DB2 All times are GMT + 6 Hours
Page 1 of 1

 

Search our Forum:

Similar Topics
Topic Author Forum Replies Posted
No new posts Insert a Row_number into table useit DB2 2 Tue Sep 19, 2017 1:07 pm
No new posts Create procedure issues -628 when add... chandraBE DB2 1 Mon Sep 18, 2017 12:16 pm
No new posts how to see when the last read access ... Mike 1304 DB2 1 Tue Sep 12, 2017 7:52 pm
This topic is locked: you cannot edit posts or make replies. Extract all "IF" Statements... Adarsh Damodaran CLIST & REXX 1 Wed Sep 06, 2017 9:28 am
No new posts PC (UTF-8) -> z/OS (EBCDIC) -> ... prino All Other Mainframe Topics 4 Fri Sep 01, 2017 1:47 am

Facebook
Back to Top
 
Job Vacancies | Forum Rules | Bookmarks | Subscriptions | FAQ | Polls | Contact Us