我试图在两个字段上组合,并返回第三个字段,该字段是前两个字段的共同值,产品ID。这里是我的数据:T-SQL如何GROUP BY两个字段并连接另一个
ProductID Currency Price Territory
1 USD 6.99 US
1 EUR 4.99 GR
1 EUR 4.99 HU
1 EUR 4.99 LT
2 USD 7.99 US
2 EUR 5.99 GR
2 EUR 5.99 HU
我想结果回来是这样的:
ProductID Currency Price Territories
1 USD 6.99 US
1 EUR 6.99 GR, HU, LT
我可以拉Currenty和价格列,但不能连接它们共有的领土:
SELECT Currency, Price
FROM TerritoryPricing
WHERE ProductID = 1
GROUP BY Currency, Price
如何连接区域?
Google:“SQL Server聚合字符串连接” –
看这篇文章http://stackoverflow.com/questions/3368942/grouped-string-aggregation-listagg-for-sql-server – are
http://sqlperformance.com/2014/08/t-sql-queries/sql-server-groupped-concatenation –