View previous topic :: View next topic
|
Author |
Message |
kushal Bothra
New User
Joined: 16 Feb 2011 Posts: 38 Location: Gurgoan
|
|
|
|
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 |
|
Back to top |
|
|
Pandora-Box
Global Moderator
Joined: 07 Sep 2006 Posts: 1592 Location: Andromeda Galaxy
|
|
|
|
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 )
Quote: |
SQL Query for select and multi[le fetch |
Please ensure to have Topic names closely related to your doubts |
|
Back to top |
|
|
dbzTHEdinosauer
Global Moderator
Joined: 20 Oct 2006 Posts: 6966 Location: porcelain throne
|
|
Back to top |
|
|
Pandora-Box
Global Moderator
Joined: 07 Sep 2006 Posts: 1592 Location: Andromeda Galaxy
|
|
|
|
Dick,
I was thinking about UNION/UNION ALL as well not very sure which is better when compared with coalesce |
|
Back to top |
|
|
dbzTHEdinosauer
Global Moderator
Joined: 20 Oct 2006 Posts: 6966 Location: porcelain throne
|
|
|
|
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 |
|
|
Pandora-Box
Global Moderator
Joined: 07 Sep 2006 Posts: 1592 Location: Andromeda Galaxy
|
|
|
|
I understand may be will try that on monday and get back on coalesce
Thanks |
|
Back to top |
|
|
kushal Bothra
New User
Joined: 16 Feb 2011 Posts: 38 Location: Gurgoan
|
|
|
|
Hi Pandora-Box/dbzTHEdinosauer
Thanks for your replies |
|
Back to top |
|
|
kushal Bothra
New User
Joined: 16 Feb 2011 Posts: 38 Location: Gurgoan
|
|
|
|
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 |
|
|
chandan.inst
Active User
Joined: 03 Nov 2005 Posts: 275 Location: Mumbai
|
|
|
|
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 |
|
|
kushal Bothra
New User
Joined: 16 Feb 2011 Posts: 38 Location: Gurgoan
|
|
|
|
Thanks Chandan
Its working for me .... |
|
Back to top |
|
|
enrico-sorichetti
Superior Member
Joined: 14 Mar 2007 Posts: 10873 Location: italy
|
|
|
|
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 |
|
Back to top |
|
|
GuyC
Senior Member
Joined: 11 Aug 2009 Posts: 1281 Location: Belgium
|
|
|
|
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 |
|
|
dbzTHEdinosauer
Global Moderator
Joined: 20 Oct 2006 Posts: 6966 Location: porcelain throne
|
|
|
|
again, thx for the lesson Guy |
|
Back to top |
|
|
chandan.inst
Active User
Joined: 03 Nov 2005 Posts: 275 Location: Mumbai
|
|
|
|
Thanks GuyC..its pretty much informative.. |
|
Back to top |
|
|
|