0

请让我知道是否可以创建这种查询。我从来没有见过这样的东西

请检查图片下方。这正是我需要做的!

在此处输入图像描述

4

1 回答 1

0
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

sql fiddle demo

如果您在其他表中有产品名称:

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
于 2013-09-09T19:35:40.557 回答