March 5, 2007 at 11:18 pm
A puzzle for SQL Masters !!!
| Table1 | |
| id | Name |
| 1 | X |
| 2 | Y |
| 3 | Z |
| 4 | A |
| 5 | C |
| 6 | x |
| Table2 | |
| id | Name |
| 1 | A |
| 2 | B |
| 4 | B |
| Table3 | |
| id | Name |
| 1 | P |
| 5 | C |
Take Id as integer and Name as varchar in all three tables
Good luck!! Regards, Sandeep | |||||||||||||||||||||||||||
March 6, 2007 at 12:06 am
Here's my quick and dirt solution, excluding the table creation and inserts code...
select
t1.id,
Name = case
when t3.id is not null then t3.[Name]
when t2.id is not null then t2.[Name]
else t1.[Name] end
from Table1 t1
left join Table2 t2 on t1.id = t2.id
left join Table3 t3 on t1.id = t3.id
Td Wilson
March 6, 2007 at 3:39 am
Hi ,
Excellent Question and very good answer by Td Wilson ...
Good One Boss..............
Regards ,
Amit Gupta...
March 6, 2007 at 3:45 am
Same result may be achieved with a little bit simpler query:
select t1.id, COALESCE (t3.[Name], t2.[Name], t1.[Name]) as Name
from Table1 t1
left join Table2 t2 on t1.id = t2.id
left join Table3 t3 on t1.id = t3.id
_____________
Code for TallyGenerator
Viewing 4 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply