Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在一条记录中累积数据的SQL组列_Sql_Sql Server 2008 - Fatal编程技术网

在一条记录中累积数据的SQL组列

在一条记录中累积数据的SQL组列,sql,sql-server-2008,Sql,Sql Server 2008,我正在尝试在SQL Server上编写一个查询,以将数据项合并到一条记录中 输入表: sales_ref_no Description 001 Hello 001 Hi 002 Dear 002 All 002 Please 003 Thanks sales_ref_no Description 001 Hello | Hi 002 Dear | All | Please 003 Thanks 输出表: sales_ref_no Description 001 Hello

我正在尝试在SQL Server上编写一个查询,以将数据项合并到一条记录中

输入表:

sales_ref_no    Description
001 Hello
001 Hi
002 Dear
002 All
002 Please
003 Thanks
sales_ref_no    Description
001 Hello | Hi
002 Dear | All | Please
003 Thanks
输出表:

sales_ref_no    Description
001 Hello
001 Hi
002 Dear
002 All
002 Please
003 Thanks
sales_ref_no    Description
001 Hello | Hi
002 Dear | All | Please
003 Thanks
同一销售参考号下的描述使用|分隔符累积在同一记录下

有人能帮忙吗


谢谢

尝试谷歌sql server字符串聚合。stackoverflow.com上有很多答案
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