I need to return records on a table and my result set needs to contain a comma separated list.
I have attached an image of the 3 tables. I need to do a select that returns the record in the first table and include the last of AwardFocusName that exist in the 3rd table in the screenshot.
So my result set would return one record and include the list of AwardFocusNames in it (comma separated).
Here's a trick I've used in the past to do similar things. Use SUBSTRING function.
Note that the 2 is used to chop off the leading comma that the subselect adds to the first item, and 1000000 is chosen as a large number to mean "all of the rest of the string".
Create a Scalar-valued Function like this
I explain how to do this on my blog, here:
http://johniekarr.wordpress.com/2011/08/08/pushing-multiple-results-into-one-column/
I think the best solution would be to create a User defined aggregate that concatenates the values (in a group) into a comma separated list. See Example 1 at: http://msdn.microsoft.com/en-us/library/ms131056.aspx
Usage: