Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/22.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.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 server 如何获取以下场景的CSV值_Sql Server_Sql Server 2005 - Fatal编程技术网

Sql server 如何获取以下场景的CSV值

Sql server 如何获取以下场景的CSV值,sql-server,sql-server-2005,Sql Server,Sql Server 2005,这是我当前的表数据 AccountNumber Product ------------------------------ 00505871 Product1 00505871 Product2 00503297 Product3 00900004 Product4 00505871 Product3 00514884 Pr

这是我当前的表数据

AccountNumber         Product
------------------------------
00505871              Product1
00505871              Product2
00503297              Product3
00900004              Product4
00505871              Product3
00514884              Product3
00503297              Product2
00505871              Product1
我怎样才能达到以下结果

AccountNumber        ProductString
------------------------------------------------
00505871             Product1,Product2,Product3
00503297             Product2,Product3
00900004             Product4
00514884             Product3
谢谢


下面的Ashish Chotalia解决方案对我有效

select AccountNumber, 
       stuff((select ',' + Product 
                  from YourTable t2 
                  where t2.AccountNumber = t1.AccountNumber 
                  order by Product 
                  for xml path('')),1,1,'') as ProductString
    from YourTable t1
    group by AccountNumber
1. 2.
这会给出结果,但我希望每个帐户都有不同的产品。@ashish.chotalia:正如您在独立发布的答案中所展示的,只需在我给您的解决方案中添加
distinct
关键字即可。你从别人那里得到了一个非常有用的答案。为什么要接受他们的答案,稍微调整一下,然后发布自己的答案并接受,而不是让他调整自己的答案?@Adam,你可以看到时间戳,这里是我遇到的两种方法。我认为它可能对社区有用,这就是将它标记为答案的原因。是的,我可以看到你在Joe发布他的答案一天后发布了它。只要在他的查询中添加
distinct
关键字就可以了。如果没有可用的答案,你应该只发布(并接受)你自己的答案。好的,我选择他的答案:)
select AccountNumber, 
       stuff((select ',' + Product 
                  from YourTable t2 
                  where t2.AccountNumber = t1.AccountNumber 
      Group by Product 
                  order by Product          
                  for xml path('')),1,1,'') as ProductString
    from YourTable t1
    group by AccountNumber
select AccountNumber,   
       stuff((select distinct ',' +Product
                  from YourTable t2   
                  where t2.AccountNumber = t1.AccountNumber   
                  group by Product   
                  for xml path('')),1,1,'') as ProductString  
    from YourTable t1  
    group by AccountNumber