teradata - Append previous transaction date SQL -
i have table shows customer_id, product_id, browse_date, purchase_date, , difference between browse , purchase date. looks this.
id pid b_date p_date 1 001 7/20/2014 7/20/2014 1 001 7/20/2014 7/20/2014 1 002 7/20/2014 7/20/2014 2 001 7/20/2014 7/20/2014 2 001 7/20/2014 8/01/2014 2 002 7/25/2014 8/01/2014 2 002 7/26/2014 8/01/2014 2 002 7/28/2014 8/01/2014 2 002 7/28/2014 8/01/2014 what efficient way append recent purchase date each customer previous latest purchase. so, result this.
id pid b_date p_date latest_purchase_date 1 001 7/20/2014 7/20/2014 'n/a' 1 001 7/20/2014 7/20/2014 'n/a' 1 002 7/20/2014 7/20/2014 'n/a' 2 001 7/20/2014 7/20/2014 'n/a' 2 001 7/20/2014 8/01/2014 7/20/2014 2 002 7/25/2014 8/01/2014 7/20/2014 2 002 7/26/2014 8/01/2014 7/20/2014 2 002 7/28/2014 8/01/2014 7/20/2014 2 002 7/28/2014 8/01/2014 7/20/2014 i'm on teradata 13.1
there's no lag in teradata, it's easy rewrite.
as there multiple rows same p_date need keep track whenever changes.
select id, pid, b_date, p_date ,max(last_dt) -- fill nulls last date on (partition id order p_date, last_dt desc rows unbounded preceding) ( select id, pid, b_date, p_date, nullif(min(p_date) -- return date when there's change, otherwise null on (partition id order p_date rows between 1 preceding , 1 preceding) , p_date) last_dt vt ) dt
Comments
Post a Comment