Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 how to achieve this output

Author  Topic 

imranabdulaziz
Yak Posting Veteran

83 Posts

Posted - 2009-08-22 : 07:26:09
Dear all,
I am using sql server 2005.

EMPCODE EMPNAME BOSSCODE XYZ XYZ
EM001 ABC EM005
EM002 DEF EM011
EM005 XYZ EM011
EM011 LCD

I want to display empcode , empname , bossname like
Em001 abc xyz

I tried with this query
SELECT ER_EMP_CODE , ER_EMP_NAME , (SELECT ER_EMP_NAME FROM emp_DTL WHERE ER_EMP_CODE = A.ER_BOSS_CODE ) FROM emp_DTL A

I got the correct result but is there any other way around to achieve the required result

cat_jesus
Aged Yak Warrior

547 Posts

Posted - 2009-08-22 : 08:47:33
select A.empcode, A.empname, coalesce(B.empname,'No Boss!')
From sometable A
left join sometable b on A.bosscode = B.Empcode

Like this?

An infinite universe is the ultimate cartesian product.
Go to Top of Page
   

- Advertisement -