我试着根据sql server中显示的结果合并以下数据。原始数据:
预期结果:
我不确定我是否应该使用Pivot。而且,当我尝试concat时,它只能合并同一行的数据。如果你能帮助我,这将是一个很大的帮助。:)
2jcobegt1#
看起来How to turn one column of a table into a csv string in SQL Server without using a cursor中带有string_agg的答案应该能解决问题
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
rt4zxlrg2#
在MYSQL中:
SELECT salesid, loyaltyid, GROUP_CONCAT(text, '+') AS paymentItem FROM ABC GROUP BY salesId, loyaltyId ORDER BY salesId, loyaltyId;
2条答案
按热度按时间2jcobegt1#
看起来How to turn one column of a table into a csv string in SQL Server without using a cursor中带有string_agg的答案应该能解决问题
https://learn.microsoft.com/en-us/sql/t-sql/functions/string-agg-transact-sql?view=sql-server-2017
rt4zxlrg2#
在MYSQL中: