我在这里阅读的许多问题/解决方案都描述了如何单独使用STRING_AGG,我可以使用以下方法:
SELECT Offers.Id, STRING_AGG ( Offers2Currencies.CurrencyCode, ', ' ) AS Currencies
FROM Offers INNER JOIN Offers2Currencies ON Offers2Currencies.OfferID =
dbo.Offers.ID
WHERE dbo.Offers.BuyerMemberId = '64ad10b9-85a6-4fc4-b9eb-d9f9af164d2b'
GROUP BY dbo.Offers.Id
但我正在努力将其放入更大的查询中,例如:
SELECT
dbo.Offers.ID,
dbo.Offers.UTC,
dbo.Organizations.Code,
dbo.Entities.EntityAbbrev,
dbo.Measurables.Name,
dbo.Offers.Price,
dbo.Offers.SellerMemberId,
dbo.AspNetUsers.UserName
--select STRING_AGG(dbo.Offers2Currencies.CurrencyCode, ', ') Currencies
FROM dbo.Offers
INNER JOIN dbo.AspNetUsers ON dbo.AspNetUsers.Id = dbo.Offers.SellerMemberId
INNER JOIN dbo.MemberCreditRatings ON dbo.AspNetUsers.Id = dbo.MemberCreditRatings.MemberGUID
INNER JOIN dbo.Measurables ON dbo.Offers.MeasurableID = dbo.Measurables.ID
INNER JOIN dbo.Entities ON dbo.Offers.EntityID = dbo.Entities.ID
INNER JOIN dbo.Organizations ON dbo.Measurables.OrganizationID = dbo.Organizations.ID
--INNER JOIN dbo.Offers2Currencies ON dbo.Offers2Currencies.OfferID = dbo.Offers.ID
AND dbo.Entities.OrganizationID = dbo.Organizations.ID
WHERE dbo.Offers.BuyerMemberId = '64ad10b9-85a6-4fc4-b9eb-d9f9af164d2b'
答案 0 :(得分:0)
我(想)我想出来了 - 将所有选择的字段放入Group By中(尽管它们永远不会超出第一组) - 请参阅http://sqlfiddle.com/#!18/aaa65/7(并建议是否更好或替代解决方案)