In our business we have a main base account and then subordinate accounts under the baseaccount.
1.) How do I get all the accounts into a single column including the base account (comma delimited)?
I've used this code before on other datasets and it works great. I just can't figure out how to make this work with all the multiple joins.
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'
My Output:
I would like to see the comma delimited list like this: 11727765, 11727799, 11783396, 12192670
I've gone through all the other questions on adding data to a single string and I can't find a solution here. Not a duplicate.