Good evening,
I have a table layout similar to the image below. The User table is storing most of my information. For each user I can also have an undetermined number of Preference and Location selections, which I store in junction tables as shown in the image.
When an administrator comes to produce a report, they will use the preference and location selections like a filter. For instance, they may select Preference1 and Preference3, and UserLocation2 and UserLocation4, and the report should show only users who have at least one of the preferences AND at least one of the locations. Is it possible to do this in one SQL select statement? I'm using SQL Server 2008 R2.
Assuming a User record qualifies for return according to the above, is there a way to show all of their Preferences and Locations, concatenated into a new column. I have figured out how to get a concatenated result back for an individual user, but wonder if it can also be done as part of the same SQL select statement.
This is what I have for returning the Preferences comma delimited, for an individual UserID...
DECLARE @concatlist VARCHAR(MAX)
SELECT @concatlist = COALESCE(@concatlist+', ', '') + dbo.Preference.Title
FROM dbo.UserPreference
LEFT OUTER JOIN dbo.Preference
ON dbo.UserPreference.PreferenceID = dbo.Preference.PreferenceID
WHERE dbo.UserPreference.UserID = 5
SELECT @concatlist as OutputColumn
Any pointers would be great. I've spent a half day on this and though I can do it in part, can't find a way to do it in one statement.
Thanks,