Quantcast
Channel: Teradata Forums - Database
Viewing all articles
Browse latest Browse all 14773

How to get 2 value from same id and same column? - response (1) by Harpreet Singh

$
0
0

this will do all except suming up price which will need to be joined to resultset

CREATE TABLE har2 AS
(
SEL id,customer, book, price, ROW_NUMBER () OVER (PARTITION BY id,customer ORDER BY book ) AS rnk FROM DLWORK_DB01_DA.har )
WITH DATA ;

WITH RECURSIVE base (c_rnk, C_id,  c_customer,c_list) AS( 
SELECT rnk c_rnk,  id AS c_id, customer AS c_customer , CAST(TRIM(book) AS VARCHAR(6000)) c_list 
FROM  har2 
WHERE rnk = 1   
UNION
 ALL  
SELECT rnk c_rnk, id, customer ,b.c_list || ',' || CAST(TRIM(c.book) AS VARCHAR(6000)) c_list 
FROM har2 c INNER JOIN base b 
 ON b.c_rnk + 1 = c.rnk  
 AND c.id=b.c_id )   
SELECT  c_id, c_customer, c_list 
FROM base 
QUALIFY RANK() OVER (PARTITION BY c_id
ORDER BY c_rnk DESC) = 1

 


Viewing all articles
Browse latest Browse all 14773

Trending Articles