为 SQL Server 2005 编辑
如果您使用的是 SQL Server:
SELECT itemid
,deliverydates = STUFF((SELECT ' ' + CONVERT(NVARCHAR, deliverydate, 101)
FROM purchline b
WHERE b.itemid = a.itemid
FOR XML PATH('')), 1, 1, '')
,TotalOrdered = SUM(qtyordered)
FROM purchline a
GROUP BY itemid
使用以下内容进行测试:
-- Setup test data
DECLARE @T1 AS TABLE (
itemid NVARCHAR(50)
,deliverydate DATETIME
,qtyordered INT);
INSERT @T1 VALUES ('74700', '20000101', 50)
INSERT @T1 VALUES ('74700', '20070101', 51)
INSERT @T1 VALUES ('74700', '20100101', 5)
-- Return the desired results
SELECT itemid
,deliverydates = STUFF((SELECT ' ' + CONVERT(NVARCHAR, deliverydate, 101)
FROM @T1 b
WHERE b.itemid = a.itemid
FOR XML PATH('')), 1, 1, '')
,TotalOrdered = SUM(qtyordered)
FROM @T1 a
GROUP BY itemid