RDBMS is SQL Server 2008.
I have 3 tables. To simplify they look like this:
NominationOrder
table:
NominationOrderId | NominationId
1 | 5
2 | 9
NominationOrderItem
table:
NominationOrderItemId | NominationOrderId | GiftId
1 | 1 | 6
2 | 1 | 3
3 | 1 | 9
Gift
table:
GiftId | GiftName |
3 | TVSet
6 | TabletPC
9 | LittlePonny
So, there's some Nomination
. Each Nomination
may have 1 Nomination Order
. Each Nomination Order
may have many Nomination Order Items
, each of them references to some Gift
from this order.
I'm making a report for Reporting Services and I need to display data about each nomination with Gift
in a single row, showing Gift
names concatenated.
Currently it looks like this:
NominationId | NominationOrderId | GiftName
5 | 1 | TVSet
5 | 1 | TabletPC
5 | 1 | LittlePonny
I need it to look like this:
NominationId | NominationOrderId | GiftName
5 | 1 | TVSet, TabletPC, LittlePonny
A simplified example of current SQL query:
select
nn.NominationId
,n_o.NominationOrderId
,g.name GiftName
from dbo.Nomination nn
LEFT JOIN dbo.NominationOrder n_o ON n_o.NominationId = nn.NominationId
LEFT JOIN dbo.NominationOrderItem noi ON noi.NominationOrderId = n_o.NominationOrderId
left join dbo.Gift g on g.GiftId = noi.GiftId
How can I rewrite it to make an output in a single string and concatenate gift names?