请让我知道是否可以创建这种查询。我从来没有见过这样的东西
请检查图片下方。这正是我需要做的!
select
A.OrderID,
A.CustomerID,
stuff(
(select ' ' + T.Product
from test as T
where T.OrderID = A.OrderID and T.CustomerID = A.CustomerID
for xml path(''), type
).value('.', 'nvarchar(max)')
, 1, 1, '') as Product
from test as A
group by A.OrderID, A.CustomerID
如果您在其他表中有产品名称:
select
A.OrderID,
A.CustomerID,
stuff(
(select ' ' + P.Name
from test as T
inner join Products as P on P.ID = T.ProductID
where T.OrderID = A.OrderID and T.CustomerID = A.CustomerID
for xml path(''), type
).value('.', 'nvarchar(max)')
, 1, 1, '') as Product
from test as A
group by A.OrderID, A.CustomerID