0

I have tried to combine below data according to the result shown as below in sql server.

Raw data:

enter image description here

Expected result:

enter image description here

i am not sure if i should use pivot. also, when i tried to concat it only able to combine the data from the same row. It such a great help if you can help me with this. :)

Amiel Echizen
  • 37
  • 1
  • 6
  • 2
    have a read of this question https://stackoverflow.com/questions/455423/how-to-turn-one-column-of-a-table-into-a-csv-string-in-sql-server-without-using – Ian Kenney Nov 09 '22 at 04:54

2 Answers2

1

Looks like the answer from How to turn one column of a table into a csv string in SQL Server without using a cursor with string_agg should do the trick

SELECT
    salesid, 
    loyaltyid, 
    STRING_AGG(paymentitem, '+') AS paymentItem
FROM rawdata 
GROUP BY salesId, loyaltyId
ORDER BY salesId, loyaltyId;

https://learn.microsoft.com/en-us/sql/t-sql/functions/string-agg-transact-sql?view=sql-server-2017

Jonas Metzler
  • 4,517
  • 1
  • 5
  • 17
Ian Kenney
  • 6,376
  • 1
  • 25
  • 44
0

In MYSQL:

SELECT
    salesid, 
    loyaltyid, 
    GROUP_CONCAT(text, '+') AS paymentItem
FROM ABC 
GROUP BY salesId, loyaltyId
ORDER BY salesId, loyaltyId;
Timus
  • 10,974
  • 5
  • 14
  • 28