如何获取以下场景的 CSV 值 [英] HOw to get CSV value for following scenario

查看:23
本文介绍了如何获取以下场景的 CSV 值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我当前的表格数据.

Here's my current table data.

AccountNumber         Product
------------------------------
00505871              Product1
00505871              Product2
00503297              Product3
00900004              Product4
00505871              Product3
00514884              Product3
00503297              Product2
00505871              Product1

我怎样才能达到以下结果.

How can I achieve following result.

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

谢谢,

Ashish Chotalia

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

这篇关于如何获取以下场景的 CSV 值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆