简体   繁体   中英

STRING_AGG with Union All

I want to do STRING_AGG with union all. here are my tables.

        SELECT
            C1 = P1
        FROM
            elt.Api


        UNION ALL 

        SELECT
            C1 = E1
        FROM
            elt.Api2

for one table STRING_AGG is working fine.

SELECT STRING_AGG(CONVERT(NVARCHAR(max), ISNULL(P1,'N/A')), ',') AS C1 FROM
    elt.Api

how should I do same with UNION ALL? Do I have to write the cursor for this?

Try Something like this

SELECT STRING_AGG(CONVERT(NVARCHAR(max), ISNULL(c1,'N/A')), ',') AS C1
FROM
(SELECT
        C1 = P1
    FROM
        elt.Api


    UNION ALL 

    SELECT
        C1 = E1
    FROM
        elt.Api2
) A

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM