Quantcast
Channel: Teradata Forums - All forums
Viewing all articles
Browse latest Browse all 27759

concatenate value of multiple rows into one Single row - response (1) by dnoeth

$
0
0

Hi Bill,
you're creating a huge intermediate spool due to the join on parent_id = parent AND child_id >child which is a kind of cross join.
Better use the following approach:

CREATE VOLATILE TABLE vt_temp AS (
 SELECT 
   Parent_ID                     
   ,Child_ID                      
   ,city_nm                       
   ,EMAIL_ADDR                    
   ,ROW_NUMBER() OVER (PARTITION BY parent_id ORDER BY child_id) AS rn
FROM temp
) WITH DATA PRIMARY INDEX(parent_id) ON COMMIT PRESERVE ROWS;


WITH RECURSIVE rec_test(parent,child, location,mail,LVL)
   AS
   (
    SELECT parent_id,child_id (VARCHAR(1000)),city_nm (VARCHAR(1000)),email_addr, 1
    FROM vt_temp
    WHERE rn = 1
    UNION ALL
    SELECT  parent_id, TRIM(child_id) || ', ' || child, TRIM(city_nm)  || ', ' || location ,email_addr,LVL+1
    FROM vt_temp INNER JOIN rec_test
    ON parent_id = parent
   AND vt_temp.rn = rec_test.lvl+1
   )
   SELECT parent,child, location,mail,LVL
   FROM rec_test
QUALIFY RANK() OVER(PARTITION BY parent ORDER BY LVL DESC) = 1;

The QUALIFY could also be replaced by doing a COUNT in the Create Table and a LVL=COUNT.
Dieter


Viewing all articles
Browse latest Browse all 27759

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>