Portal | Manuals | References | Downloads | Info | Programs | JCLs | Master the Mainframes
IBM Mainframe Computers Forums Index
 
Register
 
IBM Mainframe Computers Forums Index Mainframe: Search IBM Mainframe Forum: FAQ Memberlist Usergroups 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 Join in SQL Query vickey_dw DB2 2 Tue Jan 17, 2017 12:15 am
This topic is locked: you cannot edit posts or make replies. MQ setup query mohitsaini Java & MQSeries 4 Sun Jan 08, 2017 1:53 am
No new posts DB2 query Using Recursion, Converting... smilewithashu2 DB2 1 Tue Jan 03, 2017 12:50 pm
No new posts SQL query to run through list of valu... Ni3-db2 DB2 14 Wed Dec 14, 2016 9:52 am
This topic is locked: you cannot edit posts or make replies. Db2 SQL query to convert rows into co... subratarec DB2 5 Thu Nov 17, 2016 4:51 pm


Facebook
Back to Top
 
Mainframe Wiki | Forum Rules | Bookmarks | Subscriptions | FAQ | Tutorials | Contact Us