I am trying to write a query on SQL Server to merge data items in 1 record
Input table:
sales_ref_no Description
001 Hello
001 Hi
002 Dear
002 All
002 Please
003 Thanks
Output table:
sales_ref_no Description
001 Hello | Hi
002 Dear | All | Please
003 Thanks
The description under the same sales_ref_no is accumulated under the same record using a | delimiter
Can anyone help?
Thanks!
Select
sales_ref_no,
STUFF((
SELECT ' | ' + B.Description
FROM YOUR_TABLE B
WHERE (B.sales_ref_no = A.sales_ref_no)
FOR XML PATH(''),TYPE).value('(./text())[1]','VARCHAR(MAX)')
,1,2,'') AS Description
From YOUR_TABLE A
Group By sales_ref_no
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加