Skip to content

Instantly share code, notes, and snippets.

@megatontech
Created October 18, 2019 00:52
Show Gist options
  • Save megatontech/9bbb10674b68e2d1ed7c88cd6f78e7b9 to your computer and use it in GitHub Desktop.
Save megatontech/9bbb10674b68e2d1ed7c88cd6f78e7b9 to your computer and use it in GitHub Desktop.
SQLSERVER拼接字符串
SELECT DISTINCT
OrderID,
supplier = STUFF(
(
SELECT DISTINCT
',' + Supplier
FROM
Ord_Order_Cost t
WHERE
OrderID = t1.OrderID
AND ItemType = 0
AND CostType = 2
GROUP BY
',' + Supplier FOR XML PATH ('')
),
1,
1,
''
),
SupplierIds = STUFF(
(
SELECT DISTINCT
',' + CONVERT (NVARCHAR(50), SupplierID)
FROM
Ord_Order_Cost t
WHERE
OrderID = t1.OrderID
AND ItemType = 0
AND CostType = 2
GROUP BY
',' + CONVERT (NVARCHAR(50), SupplierID) FOR XML PATH ('')
),
1,
1,
''
)
FROM
Ord_Order_Cost t1
GROUP BY
t1.OrderID,
supplier,
SupplierID
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment