Click here to Skip to main content
16,017,137 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
Employee  
  Id   Name 
   1    abc
   2    xyz

Department

DId    DName
1       D1
2       D2

EmpDepart

EmpDid     DId
   1         1
   2         1
   3         2

i need the output 

Name      DName
abc        D1
xyz        D1,D2
Posted
Updated 27-Jul-12 16:57pm
v2
Comments
[no name] 27-Jul-12 16:32pm    
Does your homework indicate what the relationship of xyz is to D1 and D2?

1 solution

Select Name,
stuff((select ','+DName
from Department as D
INNER JOIN EMpDepartment ED on ED.DID=D.DID
where E.Id = ED.ID
for xml path(''), type).value('.', 'nvarchar(max)'), 1, 1, '') as DNAME
from Employee as E
group by E.ID
order by E.ID
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900