0

Suppose you have a query result like:

enter image description here

The Groupnames for order 128770 are A, B and C and they all display in separate rows. The same for order 128772 which has groupname B and X. I would like to display the distinct groupname values per order in one row on alfabetic order. The separator in the example is a comma, but it can be a + or - or anything. In the example it would return this result:

enter image description here

How can this be done in TSQL? It probably has to find the number of rows per order, get all those distinct groupnames, then add them together in one string for the first (or last) row in the order. Or it has to add a distinct group to each row in the order. But how do you do this?

The query example is:

    SELECT o.Ordernr, g.Group
    FROM Orders o INNER JOIN Groups g ON o.Itemcode = g.Itemcode
J3FFK
  • 664
  • 3
  • 14
  • 32
  • 1
    [http://stackoverflow.com/questions/17591490/how-to-make-a-query-with-group-concat-in-sql-server][1] [1]: http://stackoverflow.com/questions/17591490/how-to-make-a-query-with-group-concat-in-sql-server try this solution – realnumber3012 May 12 '14 at 08:18
  • Thanks, that's what I was looking for. It uses a subselect with FOR XML path. Interesting! – J3FFK May 12 '14 at 08:28

0 Answers0