【问题标题】:how to get all the accounts belonging to a particular Id? [duplicate]如何获取属于特定 ID 的所有帐户? [复制]
【发布时间】:2019-04-30 11:50:02
【问题描述】:

我正在使用 Mssql :-

我有一个这样的表,我们称之为 accTable

ID    | Account
--------------
  1   |  9019
  1   |  9020
  2   |  2010
  2   |  9999
  3   |  2323
--------------

我想要一个查询,它会给我如下输出表???

ID    | Account
--------------
  1   |  9019,9020
  2   |  2010,9999
  3   |  2323
--------------

【问题讨论】:

  • 什么关系型数据库?你需要任何 GROUP_CONCAT/STRING_AGG/LISTAGG 等价物。

标签: sql sql-server tsql string-aggregation


【解决方案1】:

使用Stufffor xml path

select ID , 
       stuff((select ','+ Account 
              from #tbl 
              where #tbl.ID     = a.ID    
              for xml path('')),1,1,'') Account
from
#tbl a
group by ID    

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-04-24
    • 2016-03-04
    • 2020-11-08
    • 1970-01-01
    • 2020-12-09
    • 1970-01-01
    • 2021-05-25
    • 1970-01-01
    相关资源
    最近更新 更多