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 needed...

 
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> DB2
View previous topic :: :: View next topic  
Author Message
Rohit Umarjikar

Senior Member


Joined: 21 Sep 2010
Posts: 1625
Location: NY,USA

PostPosted: Wed Jan 16, 2013 6:30 am    Post subject: Query needed...
Reply with quote

Hello Team,

I have below two tables,

A
Code:

DepNo
---------------
12345
12346
12347



B
Code:


DepNo            flag
---------------  -------
12345              H
12345              H
12345              S
12345              I
12347              I
12347              S


I would like to output as,

output
Code:

DepNo           Flag
--------------  -------
12345            H
12346            N
12347            S


Here below rules are places,
1)All the DepNo from table A should come
2)If the DepNo from A is not present in table B then flag should be marked as 'N'
3)if DeptNo is present multiple times in table B (for e.g. 12345 ) then out of I,S,H only H should be picked up also if only S,I is present(for e.g. 12347) then 'S' should be picked up and if any 'I' is present then only 'I' should be picked up.

Appreciate your suggestion.
Back to top
View user's profile Send private message

Srihari Gonugunta

Active User


Joined: 14 Sep 2007
Posts: 295
Location: Singapore

PostPosted: Wed Jan 16, 2013 1:04 pm    Post subject:
Reply with quote

Rohit,
You may try this.

Code:
select distinct t1.depno,
case
when locate('H',t1.flag)<>0 then 'H'
when locate('S',t1.flag)<>0 then 'S'
when locate('I',t1.flag)<>0 then 'I'
else 'N'end as flag
from (select distinct a.depno,coalesce(b.flag,'N')||coalesce(c.flag,'N')||coalesce(d.flag,'N') as flag from table1 a
left outer join table2 b on a.depno = b.depno and b.flag = 'H'
left outer join table2 c on a.depno = c.depno and c.flag = 'S'
left outer join table2 d on a.depno = d.depno and d.flag = 'I') t1;
Back to top
View user's profile Send private message
GuyC

Senior Member


Joined: 11 Aug 2009
Posts: 1278
Location: Belgium

PostPosted: Wed Jan 16, 2013 7:32 pm    Post subject:
Reply with quote

Code:
select A.deptno,
 coalesce(substr(min(digits(locate(b.flag,'HSI'))||b.flag),11),'N')
from      TableA A
left join TableB B on a.deptno = b.deptno
group by a.deptno
Back to top
View user's profile Send private message
Rohit Umarjikar

Senior Member


Joined: 21 Sep 2010
Posts: 1625
Location: NY,USA

PostPosted: Wed Jan 16, 2013 9:57 pm    Post subject:
Reply with quote

Thanks Srihari.
GuyC, it is working as expected. Thank you.
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