September 5, 2011 at 3:41 am
I have two tables,
EmployeeDetails
EmployeeIDNameDepartmentManager
7James BondKicking buttM
1MKicking buttUnknown
10MoneyPennyHospitalityM
6QR&DM
And EmployeeEmailAddresses
EmployeeIDEmailAddress
7james_bond@mi6.com
7jbond@mi6.com
7jamesb@britishspy.com
Now I want to run a single query that’ll give me,
EmployeeIDNameDepartmentManagerEmailAddresses
7James BondKicking buttMjames_bond@mi6.com, jbond@mi6.com, jamesb@britishspy.com
1MKicking buttUnknown
10MoneyPennyHospitalityM
6QR&DM
Any ideas on how to get it?
September 5, 2011 at 4:04 am
SELECT d.EmployeeID,d.Name,d.Department,d.Manager,
STUFF(((SELECT ', '+e.EmailAddress AS "text()"
FROM EmployeeEmailAddresses e
WHERE e.EmployeeID=d.EmployeeID
ORDER BY e.EmailAddress
FOR XML PATH(''),TYPE).value('./text()[1]','VARCHAR(1000)')),1,2,'') AS EmailAddresses
FROM EmployeeDetails d
ORDER BY d.Name;
____________________________________________________
Deja View - The strange feeling that somewhere, sometime you've optimised this query before
How to get the best help on a forum
http://www.sqlservercentral.com/articles/Best+Practices/61537September 5, 2011 at 4:40 am
Thanks Mark-101232!
Works like a charm!
Viewing 3 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply
This website stores cookies on your computer.
These cookies are used to improve your website experience and provide more personalized services to you, both on this website and through other media.
To find out more about the cookies we use, see our Privacy Policy