If I run the following query on my database I get the results pasted below:
SELECT t.id as ID,
j.text as Text,
l.location as Location,
FROM [text] t WITH (nolock)
INNER JOIN Text_Location tl WITH (nolock) on tl.textid = t.id
INNER JOIN Location l WITH (nolock) on tl.locationId = l.id
ID Text Location
---------- ---------- ----------
1 FC London
1 FC Paris
2 KG Berlin
3 TX1 Roma
3 TX1 Vienna
3 TX1 Dublin
How could I get the same results grouped by ID and having different columns for location? I would like to get something like this:
ID TEXT LOCATION1 LOCATION2 LOCATION3
---------- ---------- ---------- ---------- ----------
1 FC London Paris
2 KG Berlin
3 TX1 Roma Vienna Dublin
Best Answer
Seems you would need to do some CASE statements along with some UNION ALL like this:
If you provide an SQLFiddle or some such we could play with it.