I am new to SQL Server 2005 so forgive me if my questions are basic, but I've moved past simple queries and I'm starting to find myself without the proper vocabulary to get some things done!
I have a query that joins three tables to extract contacts and their person types.
If the user has multiple person types it looks like:
LastName FirstName PersonType
Smith John Manager
Smith John Programmer
Smith John Tech
What I need is to return one row per contact, with a field that has the person type listed in it, for example:
LastName FirstName PersonTypes
Smith John Manager, Programmer, Tech
How do I go about creating the field of concatenated PersonTypes?
I figured I should run the following query and then for each record run another query to grab all the types and concatenate them into a variable, then insert the values into a temp table or table variable. In any case, how do I do this?
I have a query that joins three tables to extract contacts and their person types.
Code:
SELECT tMailList.MailListKey, tMailList.LastName, tMailList.FirstName, tPersonTypes.PersonTypeDesc
FROM tMailList JOIN tMailListPersonTypes ON tMailListPersonTypes.MailListKey = tMailList.MailListKey
JOIN tPersonTypes ON tMailListPersonTypes.PersonTypeKey = tPersonTypes.PersonTypeKey
ORDER BY tMailList.LastName, tMailList.FirstName;
If the user has multiple person types it looks like:
LastName FirstName PersonType
Smith John Manager
Smith John Programmer
Smith John Tech
What I need is to return one row per contact, with a field that has the person type listed in it, for example:
LastName FirstName PersonTypes
Smith John Manager, Programmer, Tech
How do I go about creating the field of concatenated PersonTypes?
I figured I should run the following query and then for each record run another query to grab all the types and concatenate them into a variable, then insert the values into a temp table or table variable. In any case, how do I do this?
Code:
SELECT DISTINCT tMailListPersonTypes.MailListKey, tMailList.LastName, tMailList.Firstname
FROM tMailListPersonTypes JOIN tMailList ON tMailListPersonTypes.MailListKey = tMailList.MailListKey
ORDER BY tMailList.LastName, tMailList.FirstName;