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

concatenate value of multiple rows into one Single row - forum topic by Bill Scott

$
0
0

Hi,
I searched through the forums and i know a similiar question has been asked and answered, but it doesnt work with my requirements. i tried writing a recursive query something like below, but the recursive query for my data is taking a whole lot of time to execute and i believe is highly skewed, is there any other way that i could achieve the below result?

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

My actual data has around 4000 rows with 62 unique Parent ID's and the top 10 counts of the Parent ID being
 
1000
952
485
292
140
77
76
76
76
69

 
Since the counts of the parent ID's are huge, the recursive query takes a whole lot of time to execute on the actual set of data, is there any other way that i can achieve a similiar result?
SOURCE DATA:

Parent ID   Child ID                     Location                    Parent_EMAIL_ADDR
1                 Sales                          Portland                     1ABC@XYZ.COM
1                 Finance                      San Francisco             1ABC@XYZ.COM
1                 CC                              New York                   1ABC@XYZ.COM
2                 Risk management      New Orleans              2DEF@XYZ.COM
2                 Healthcare                 Chicago                      2DEF@XYZ.COM
3                 Finance                      Salem                         3GHI@XYZ.COM
3                 CC                              Los Angeles               3GHI@XYZ.COM
4                 Sales                          Houston                     4JKL@XYZ.COM

Expected Output

Parent ID   Child ID                                       Location                                         Parent_EMAIL_ADDR
1                 Sales , Finance, CC                      Portland,San Francisco, New York   1ABC@XYZ.COM
2                 Risk management, Healthcare     New Orleans, Chicago                     2DEF@XYZ.COM
3                 Finance, CC                                  Salem, Los Angeles                         3GHI@XYZ.COM
4                 Sales                                            Houston                                          4JKL@XYZ.COM

 
Thanks,
Bill

Forums: 

Viewing all articles
Browse latest Browse all 27759

Trending Articles