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
 

 

SQL Query for select and multi[le fetch

 
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> DB2
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    Post subject: SQL Query for select and multi[le fetch
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

Moderator


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

PostPosted: Sat Jun 09, 2012 6:38 pm    Post subject:
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    Post subject:
Reply with quote

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

Moderator


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

PostPosted: Sat Jun 09, 2012 8:30 pm    Post subject:
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    Post subject:
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

Moderator


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

PostPosted: Sun Jun 10, 2012 12:00 am    Post subject:
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    Post subject:
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    Post subject:
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: 269
Location: Mumbai

PostPosted: Mon Jun 11, 2012 11:52 am    Post subject:
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    Post subject:
Reply with quote

Thanks Chandan

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

Global Moderator


Joined: 14 Mar 2007
Posts: 10202
Location: italy

PostPosted: Mon Jun 11, 2012 2:13 pm    Post subject: Reply to: SQL Query for select and multi[le fetch
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: 1278
Location: Belgium

PostPosted: Wed Jun 13, 2012 7:50 pm    Post subject:
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    Post subject:
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: 269
Location: Mumbai

PostPosted: Thu Jun 14, 2012 9:07 am    Post subject:
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    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
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
No new posts Two Selects in a single select query Rohit Umarjikar DB2 1 Fri Nov 04, 2016 8:46 pm
No new posts Problem in Running Query via JCL vickey_dw DB2 3 Tue Oct 18, 2016 11:11 pm
No new posts SQL Query optimization. arunsoods DB2 12 Sun Sep 04, 2016 6:27 am
No new posts Multi row fetch - "for read only... Nileshkul DB2 3 Sun Aug 14, 2016 12:52 am


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