0

I have a pretty complex query that returns information like this:

Image1

I would like to group all columns like this:

image2

Is there a simple way to do it?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
cdufour
  • 15
  • 3
  • In SQL Server 2016, no there's no "simple" way, you would probably need to use `FOR XML PATH` and `STUFF`, there's loads of examples of this on the site already. https://stackoverflow.com/questions/31211506/how-stuff-and-for-xml-path-work-in-sql-server – Richard Hansell Apr 10 '20 at 12:18
  • You're right. Examples are only for 1 column. I don't understand how to do it on several. – cdufour Apr 10 '20 at 12:20
  • I will sling you a quick answer together, two ticks – Richard Hansell Apr 10 '20 at 12:21

2 Answers2

0

This would be one way of getting your result:

SELECT 
   t.ID,
   STUFF((SELECT distinct ', ' + t.InternalRef
          FROM #t t
          FOR XML PATH('')), 1, 1, '') [InternalRef],

   STUFF((SELECT distinct ', ' + t.ExternalRef
   FROM #t t
   FOR XML PATH('')), 1, 1, '') [ExternalRef],

   STUFF((SELECT distinct ', ' + t.ComposantIdentifier
   FROM #t t
   FOR XML PATH('')), 1, 1, '') [ComposantIdentifier]
FROM #t t
GROUP BY t.ID
ORDER BY 1

You can try a demo of this here.

Rigerta
  • 3,959
  • 15
  • 26
0

Here you go:

WITH cte AS (
    SELECT 368519 AS Id, '243625378R' AS InternalRef, '492-90-113' AS ExternalRef, '1749MA' AS ComponentIdentifier
    UNION ALL
    SELECT 368519 AS Id, '243625378R' AS InternalRef, '492-90-114' AS ExternalRef, '1749MA' AS ComponentIdentifier
    UNION ALL
    SELECT 368519 AS Id, '(8200945715)' AS InternalRef, '(7807965)' AS ExternalRef, '1749MA' AS ComponentIdentifier)
SELECT
    Id,
    STUFF((SELECT ',' + InternalRef FROM cte FOR XML PATH ('')), 1, 1, ''),
    STUFF((SELECT ',' + ExternalRef FROM cte FOR XML PATH ('')), 1, 1, ''),
    ComponentIdentifier
FROM
    cte
GROUP BY
    Id,
    ComponentIdentifier;

The assumption is that cte, the common-table expression is the output from your existing query. So you could wrap this up in a common-table expression (replacing my UNION statement), put it into a temporary table, a table variable, etc.

I'm also making some assumptions that might be wrong, but my output matches yours:

Id      (No column name)                    (No column name)        ComponentIdentifier
368519  243625378R,243625378R,(8200945715)  492-90-113,492-90-114,(7807965) 1749MA
Richard Hansell
  • 5,315
  • 1
  • 16
  • 35
  • Great ! this is exactly what i was looking for. Thanks a lot ! – cdufour Apr 10 '20 at 12:39
  • 2
    Just one thing I forgot to note; sometimes people here get a bit *enraged* when they see images instead of text. I went to copy your data, and then realised I would have to type it in manually instead. It's not a biggie, but maybe something to bear in mind next time you post a question? Ideally you would provide a query that produces your test data, but I know that's not always possible ;) – Richard Hansell Apr 10 '20 at 12:49