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

SQL Query for select and multi[le fetch


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

New User


Joined: 16 Feb 2011
Posts: 38
Location: Gurgoan

PostPosted: Sat Jun 09, 2012 6:27 pm
Reply with quote

Hi All,

I need to write sql query for below requirement

say I have 3 tables Table A, B and C

I have to select employeeNum from A searchfor employeeName in B if found have to use that or if not found search employeeName in table C.

The requirement should be in one query.

Please help.

Thanks
Kushal icon_confused.gif
Back to top
View user's profile Send private message
Pandora-Box

Global Moderator


Joined: 07 Sep 2006
Posts: 1592
Location: Andromeda Galaxy

PostPosted: Sat Jun 09, 2012 6:38 pm
Reply with quote

Looks like an Interview question

What have you tried so far to acheive the same???

and what should the SQL do when employee names are found from both the table B & C for same employee number and what should it do when same employee names are found in B & C for same employee number?? ( Should duplicates be eliminated or duplicates be retained) ( Hope it would give you some hint icon_cool.gif )

Quote:
SQL Query for select and multi[le fetch


Please ensure to have Topic names closely related to your doubts
Back to top
View user's profile Send private message
dbzTHEdinosauer

Global Moderator


Joined: 20 Oct 2006
Posts: 6966
Location: porcelain throne

PostPosted: Sat Jun 09, 2012 8:04 pm
Reply with quote

Maybe the COALESCE function
Back to top
View user's profile Send private message
Pandora-Box

Global Moderator


Joined: 07 Sep 2006
Posts: 1592
Location: Andromeda Galaxy

PostPosted: Sat Jun 09, 2012 8:30 pm
Reply with quote

Dick,

I was thinking about UNION/UNION ALL as well not very sure which is better when compared with coalesce
Back to top
View user's profile Send private message
dbzTHEdinosauer

Global Moderator


Joined: 20 Oct 2006
Posts: 6966
Location: porcelain throne

PostPosted: Sat Jun 09, 2012 11:52 pm
Reply with quote

you have 1 column, 2 source (tables). tab2 and tab3

depending upon which has precedence when both are there,
controls your algorithm.
when both are there
then take first:
SELECT COALESCE(Tab2.NAME,Tab3.Name,'NO NAME')

or

when both are there
then take second:
SELECT COALESCE(Tab3.NAME,Tab2.Name,'NO NAME')

wack that on top of 2 JOINs.

probably a million different ways (and better, but i think that would work)

UNION with HAVING to filter out NULL would work,
but what to do when both or neither?
i could not think of a way to UNION.
and maybe my COALESCE won't work either, (no place to test).
Back to top
View user's profile Send private message
Pandora-Box

Global Moderator


Joined: 07 Sep 2006
Posts: 1592
Location: Andromeda Galaxy

PostPosted: Sun Jun 10, 2012 12:00 am
Reply with quote

I understand may be will try that on monday and get back on coalesce

Thanks icon_biggrin.gif
Back to top
View user's profile Send private message
kushal Bothra

New User


Joined: 16 Feb 2011
Posts: 38
Location: Gurgoan

PostPosted: Mon Jun 11, 2012 9:42 am
Reply with quote

Hi Pandora-Box/dbzTHEdinosauer

Thanks for your replies
Back to top
View user's profile Send private message
kushal Bothra

New User


Joined: 16 Feb 2011
Posts: 38
Location: Gurgoan

PostPosted: Mon Jun 11, 2012 10:08 am
Reply with quote

Hi

I am trying

"SELECT Coalesce( B.employeeName, C.employeeName)

where A.employeeNum = B.employeeNum
AND A.employeeNum = C.employeeNum "

As my requirement is I take employeeNum of A search in B if found return the value if not found I search in C if found return.

So the above query will only work when we have employeeNum in both A and B and it dosent seems good.

Please suggest what can be possible resolution.
Back to top
View user's profile Send private message
chandan.inst

Active User


Joined: 03 Nov 2005
Posts: 275
Location: Mumbai

PostPosted: Mon Jun 11, 2012 11:52 am
Reply with quote

Hi Khushal,

Try below query

Code:

SELECT Coalesce( B.employeeName, C.employeeName, 'NO NAME')
FROM A
         LEFT OUTER JOIN
          B ON  A.employeeNum = B.employeeNum
         LEFT OUTER JOIN
         C ON  A.employeeNum = C.employeeNum


If it does not found in both B and C it will give as 'NO NAME'

This is untested but hopefully it will work

Let us know if it works

Regards,
Chandan
Back to top
View user's profile Send private message
kushal Bothra

New User


Joined: 16 Feb 2011
Posts: 38
Location: Gurgoan

PostPosted: Mon Jun 11, 2012 2:09 pm
Reply with quote

Thanks Chandan

Its working for me .... icon_lol.gif
Back to top
View user's profile Send private message
enrico-sorichetti

Superior Member


Joined: 14 Mar 2007
Posts: 10873
Location: italy

PostPosted: Mon Jun 11, 2012 2:13 pm
Reply with quote

You said thanks to the wrong person...
the suggestion had already been given in a previous post by dbzTHEdinosauer
Quote:
depending upon which has precedence when both are there,
controls your algorithm.
when both are there
then take first:
SELECT COALESCE(Tab2.NAME,Tab3.Name,'NO NAME')

or

when both are there
then take second:
SELECT COALESCE(Tab3.NAME,Tab2.Name,'NO NAME')

wack that on top of 2 JOINs.


but You did not notice it !
or You were just waiting to be spoon fed with a ready to run solution icon_cool.gif
Back to top
View user's profile Send private message
GuyC

Senior Member


Joined: 11 Aug 2009
Posts: 1281
Location: Belgium

PostPosted: Wed Jun 13, 2012 7:50 pm
Reply with quote

It's pretty important to have left joins and not just inner joins.
Also this query will always try to join with table C even when it already found something in Table B.
IF it is only one column you need to find, you can solve it this way :

Code:
select A.employeeNum
, coalesce( select B.employeeName from TableB B where B.employeeNum = A.employeeNum,
            select C.employeeName from TableC C where C.employeeNum = A.employeeNum,
            'No Name")
from TableA A
Back to top
View user's profile Send private message
dbzTHEdinosauer

Global Moderator


Joined: 20 Oct 2006
Posts: 6966
Location: porcelain throne

PostPosted: Wed Jun 13, 2012 7:53 pm
Reply with quote

again, thx for the lesson Guy
Back to top
View user's profile Send private message
chandan.inst

Active User


Joined: 03 Nov 2005
Posts: 275
Location: Mumbai

PostPosted: Thu Jun 14, 2012 9:07 am
Reply with quote

Thanks GuyC..its pretty much informative..
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 RC query -Time column CA Products 3
No new posts Dynamically pass table name to a sele... DB2 2
No new posts Fetch data from programs execute (dat... DB2 3
No new posts Query on edit primary command CLIST & REXX 5
No new posts Query on edit primary command CLIST & REXX 1
Search our Forums:

Back to Top