Forum

Posted by saikripa, June 22, 2025, 6:06 p.m.

Qs 228 : https://sqlpad.io/questions/228/analyzing-sequential-purchase-behavior/

I feel like the results from my query are correct rather than the expected results. The expected results contains duplicate rows causing a mismatch. I am not able to view the official solution but would like to enquire/ get some guidance. 
here's my solution: 
with customer_purchase as (SELECT
    s.customer_id,
    s.sale_date,
    p.product_name,
    s.quantity * p.price as amount,
    lag(s.quantity * p.price) over (partition by s.customer_id order by s.sale_date) as lower_product_amount,
    lag(p.product_name) over (partition by s.customer_id order by s.sale_date) as lower_product_name,
    lag(s.sale_date) over (partition by s.customer_id order by s.sale_date) as lower_sale_date
                           
from msft_sales s
join msft_products p
on s.product_id = p.product_id
                           )
                           
select
    t2.name,
    t1.lower_product_name,
    t1.product_name as higher_product_name,
    t1.lower_sale_date,
    t1.sale_date as higher_value_sale_date,
    ((t1.amount - t1.lower_product_amount)* 100.0 / t1.lower_product_amount) as perc_increase
from customer_purchase t1
join msft_customers t2
on t1.customer_id = t2.customer_id
where amount >= 1.5*lower_product_amountwith customer_purchase as (SELECT
    s.customer_id,
    s.sale_date,
    p.product_name,
    s.quantity * p.price as amount,
    lag(s.quantity * p.price) over (partition by s.customer_id order by s.sale_date) as lower_product_amount,
    lag(p.product_name) over (partition by s.customer_id order by s.sale_date) as lower_product_name,
    lag(s.sale_date) over (partition by s.customer_id order by s.sale_date) as lower_sale_date
                           
from msft_sales s
join msft_products p
on s.product_id = p.product_id
                           )
                           
select
    t2.name,
    t1.lower_product_name,
    t1.product_name as higher_product_name,
    t1.lower_sale_date,
    t1.sale_date as higher_value_sale_date,
    ((t1.amount - t1.lower_product_amount)* 100.0 / t1.lower_product_amount) as perc_increase
from customer_purchase t1
join msft_customers t2
on t1.customer_id = t2.customer_id
where amount >= 1.5*lower_product_amount

This question is still open. Write your answer.