Home > Mobile >  STRING_AGG with Union All
STRING_AGG with Union All

Time:02-10

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 ?

CodePudding user response:

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
  • Related