0

Pls, let me know if is possible to create this kind of query. I never saw something like that

Pls, check below the image. It´s exactly what I need to do!

enter image description here

Rikalous
  • 4,514
  • 1
  • 40
  • 52
Rodrigo Abib
  • 83
  • 1
  • 2
  • 14
  • 1
    Are you generating a report? Any query result from a database has a 'cell' for each intersection of a row and column, even if that causes duplicated information (to human eyes) - and, unfortunately, there's just nothing you can do to change that. – Rikalous Sep 09 '13 at 19:35

1 Answers1

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

If you have Product names in other table:

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
Roman Pekar
  • 107,110
  • 28
  • 195
  • 197