SQL逗号分隔列表为单个字符串

时间:2016-04-08 22:12:30

标签: sql sql-server tsql

在我们的业务中,我们有一个主基础账户,然后是baseaccount下的附属账户 1.)如何将所有帐户都包含在一个列中,包括基本帐户(以逗号分隔)?

我之前在其他数据集上使用过此代码,效果很好。我无法弄清楚如何使用所有多个连接来完成这项工作。

    SELECT DISTINCT
    A.acctnbr as baseacctnbr, 
        STUFF((SELECT ', '+c1.ACCTNBR
FROM [USBI].[vw_FirmAccount] a1
    inner join [USBI].[vw_RelatedAccount] b1 on a1.firmaccountid = b1.basefirmaccountid 
    inner join [USBI].[vw_FirmAccount] a2 on a2.firmaccountid = b1.relatedfirmaccountid
    inner join USBI.vw_NameAddressBase c1 on b1.relatedfirmaccountid = c1.firmaccountid
        where c1.AcctNbr = c.ACCTNBR
        FOR XML PATH ('')),1,1, '') AS ALLACCTS

FROM [USBI].[vw_FirmAccount] a
    inner join [USBI].[vw_RelatedAccount] b on a.firmaccountid = b.basefirmaccountid 
    inner join [USBI].[vw_FirmAccount] a1 on a1.firmaccountid = b.relatedfirmaccountid
    inner join USBI.vw_NameAddressBase c on b.relatedfirmaccountid = c.firmaccountid

where a.acctnbr = '11727765'
  and c.restrdate <> '99999999'
  and c.closerestrictind <> 'c'
  and c.iscurrent = '1'
  and b.iscurrent = '1'

我的输出:

enter image description here

我希望看到逗号分隔的列表,如下所示:11727765, 11727799, 11783396, 12192670

我已经解决了将数据添加到单个字符串的所有其他问题,但我无法在此处找到解决方案。不重复。

2 个答案:

答案 0 :(得分:0)

您可以右键单击左上角(baseacctnbr左侧和1上方)的框并将结果另存为csv文件,然后使用记事本打开它以便以此方式查看。

就个人而言,我个人使用python很多。使用pyodbc(https://code.google.com/archive/p/pyodbc/wikis/GettingStarted.wiki),您可以执行查询并直接访问数据。对于以下代码,您所要做的就是在连接字符串

中键入服务器和数据库名称
import pyodbc

connection = pyodbc.connect('DRIVER={SQL Server Native Client11.0};SERVER=YOURSERVERHERE;DATABASE=YOURDATABASENAMEHERE;TRUSTED_CONNECTION=yes')

cursor = connection.cursor()

query = """
SELECT DISTINCT
A.acctnbr as baseacctnbr, 
    STUFF((SELECT ', '+c1.ACCTNBR
FROM [USBI].[vw_FirmAccount] a1
    inner join [USBI].[vw_RelatedAccount] b1 on a1.firmaccountid = b1.basefirmaccountid 
inner join [USBI].[vw_FirmAccount] a2 on a2.firmaccountid = b1.relatedfirmaccountid
inner join USBI.vw_NameAddressBase c1 on b1.relatedfirmaccountid = c1.firmaccountid
    where c1.AcctNbr = c.ACCTNBR
    FOR XML PATH ('')),1,1, '') AS ALLACCTS

FROM [USBI].[vw_FirmAccount] a
inner join [USBI].[vw_RelatedAccount] b on a.firmaccountid = b.basefirmaccountid 
inner join [USBI].[vw_FirmAccount] a1 on a1.firmaccountid = b.relatedfirmaccountid
inner join USBI.vw_NameAddressBase c on b.relatedfirmaccountid = c.firmaccountid

where a.acctnbr = '11727765'
  and c.restrdate <> '99999999'
  and c.closerestrictind <> 'c'
  and c.iscurrent = '1'
  and b.iscurrent = '1'
"""
cursor.execute(query)
resultList = [row[0] for row in cursor.fetchall()]
print ", ".join(str(i) for i in resultList)

答案 1 :(得分:0)

以下是我使用查询在单个字符串或单个列中创建逗号分隔列表来解决此问题的方法。我不得不使用XML Path在带有子字符串的CTE中编写代码。它适用于我需要的东西,我将在SSRS报告中使用该代码。

 ;with t1 as
(
SELECT b.relatedfirmaccountid, b.basefirmaccountid, c.firmaccountid, a.acctnbr as baseacctnbr, a1.acctnbr as relatedacct,
rn = row_number() over (partition by a.acctnbr order by a.acctnbr),
    substring((select ', '+c.acctnbr as 'data()' 
FROM [USBI].[vw_FirmAccount] a
    inner join [USBI].[vw_RelatedAccount] b on a.firmaccountid = b.basefirmaccountid 
    inner join [USBI].[vw_FirmAccount] a1 on a1.firmaccountid = b.relatedfirmaccountid
    inner join USBI.vw_NameAddressBase c on b.relatedfirmaccountid = c.firmaccountid
where a.acctnbr = '11727765'
    and c.restrdate <> '99999999'
    and c.closerestrictind <> 'c'
    and c.iscurrent = '1'
    and b.iscurrent = '1'
for xml path('')),2,255) as "AllRelatedAccts"

FROM [USBI].[vw_FirmAccount] a
    inner join [USBI].[vw_RelatedAccount] b on a.firmaccountid = b.basefirmaccountid 
    inner join [USBI].[vw_FirmAccount] a1 on a1.firmaccountid = b.relatedfirmaccountid
    inner join USBI.vw_NameAddressBase c on b.relatedfirmaccountid = c.firmaccountid
where a.acctnbr = '11727765'
    and c.restrdate <> '99999999'
    and c.closerestrictind <> 'c'
    and c.iscurrent = '1'
    and b.iscurrent = '1'
 ),
 t2 as
 (
select distinct a.acctnbr, a.name
from USBI.vw_NameAddressBase a
where a.acctnbr = '11727765'
    and a.restrdate <> '99999999'
    and a.closerestrictind <> 'c'
    and a.iscurrent = '1'
group by a.acctnbr, a.name
  )
select 
    a.baseacctnbr,
    a.relatedacct,
    a.allrelatedaccts,
    a.rn
from t1 a
inner join t2 b on a.baseacctnbr = b.acctnbr
--where rn = 1

我的结果:

enter image description here

我的结果更改了我的位置rn = 1

enter image description here