Forum

Posted by Raj, June 8, 2024, 10:53 p.m.

288. Analyzing Sequential Purchase Behavior(https://sqlpad.io/questions/228/analyzing-sequential-purchase-behavior/)

For this question, why are there duplicate values in the results. In the solution code, the following output results: 

 

Foe many customers such as Ada Lovelace, Bill Gates, Margaret Hamilton, Steve Balmer, and Tim-Berners Lee, there is a duplicate percentage increase for these customers. 

I would like to know why this exists when this is occuring for the same sale.

Additionally, when calculating the purchase for this question, shouldn't it be price * quantity instead of just using price?

The following was my code for the question:

 

WITH sales_with_lag AS (
    SELECT 
        s.customer_id,
        s.sale_id,
        s.product_id,
        s.sale_date,
        s.quantity,
        s.quantity * p.price AS sale_value,
        LAG(s.quantity * p.price) OVER (PARTITION BY s.customer_id ORDER BY s.sale_date) AS prev_sale_value,
        LAG(s.sale_id) OVER (PARTITION BY s.customer_id ORDER BY s.sale_date) AS prev_sale_id,
        LAG(s.sale_date) OVER (PARTITION BY s.customer_id ORDER BY s.sale_date) AS prev_sale_date,
        LAG(s.product_id) OVER (PARTITION BY s.customer_id ORDER BY s.sale_date) AS prev_product_id,
        ((s.quantity * p.price) - LAG(s.quantity * p.price) OVER (PARTITION BY s.customer_id ORDER BY s.sale_date)) / 
        LAG(s.quantity * p.price) OVER (PARTITION BY s.customer_id ORDER BY s.sale_date) * 100 AS percentage_increase
    FROM msft_sales s
    JOIN msft_products p ON s.product_id = p.product_id
)
SELECT 
    c.name AS customer_name,
    pp.product_name AS lower_value_product_name,
    cp.product_name AS higher_value_product_name,
    swl.prev_sale_date AS lower_value_sale_date,
    swl.sale_date AS higher_value_sale_date,
    swl.percentage_increase
FROM sales_with_lag swl
JOIN msft_customers c ON swl.customer_id = c.customer_id
JOIN msft_products cp ON swl.product_id = cp.product_id
JOIN msft_products pp ON swl.prev_product_id = pp.product_id
WHERE swl.percentage_increase >= 50
ORDER BY c.name, swl.sale_date;

 

Please let me know why the solution is the way that it is. 

customer_name lower_value_product_name higher_value_product_name lower_value_sale_date higher_value_sale_date percentage_increase
Ada Lovelace Microsoft Keyboard Visual Studio Professional 2024-01-03 2024-01-05 985.0184822787562500
Ada Lovelace Microsoft Keyboard Microsoft Teams Headset 2024-01-15 2024-01-17 95.67297238530115242400
Ada Lovelace Microsoft Keyboard Microsoft Teams Headset 2024-01-15 2024-01-17 95.67297238530115242400
Ada Lovelace Microsoft Teams Headset Azure Cloud Service Credits 2024-01-17 2024-01-24 122.2469163240360000
Ada Lovelace Microsoft Keyboard Azure Cloud Service Credits 2024-01-27 2024-01-29 334.8771472059143300
Alan Turing Halo Infinite Game Surface Pro 2024-01-13 2024-01-17 1566.9278213035505900
Alan Turing LinkedIn Premium Subscription Surface Pro 2024-01-26 2024-01-29 3234.4114704901633900
Bill Gates Azure Cloud Service Credits Xbox Series X 2024-01-11 2024-01-13 149.9950000000000000
Bill Gates Azure Cloud Service Credits Xbox Series X 2024-01-11 2024-01-13 149.9950000000000000
Bill Gates Microsoft Office 365 Subscription Xbox Series X 2024-01-20 2024-01-22 400.0400040004000400
Bill Gates Microsoft Office 365 Subscription Azure Cloud Service Credits 2024-01-25 2024-01-29 100.0200020002000200
Gabe Newell Microsoft Keyboard Surface Pro 2024-01-11 2024-01-18 2074.3639921722113500
Gabe Newell Microsoft Office 365 Subscription Visual Studio Professional 2024-01-21 2024-01-21 399.0499049904990500
Gabe Newell Windows 11 License Visual Studio Professional 2024-01-23 2024-01-25 256.4540324308879200
Gabe Newell Visual Studio Professional Surface Pro 2024-01-25 2024-01-30 100.3987975951903800
Grace Hopper Microsoft Teams Headset Azure Cloud Service Credits 2024-01-06 2024-01-08 122.2469163240360000
Grace Hopper Bing Ads Credits Azure Cloud Service Credits 2024-01-16 2024-01-18 100.00000000000000000000
Grace Hopper Halo Infinite Game Bing Ads Credits 2024-01-25 2024-01-28 66.69444907484580763500
Grace Hopper Halo Infinite Game Azure Cloud Service Credits 2024-01-30 2024-01-31 233.3888981496916200
Margaret Hamilton Microsoft Mouse Microsoft Keyboard 2024-01-14 2024-01-16 76.95267410542516352400
Margaret Hamilton Microsoft Mouse Microsoft Keyboard 2024-01-14 2024-01-16 76.95267410542516352400
Margaret Hamilton Microsoft Keyboard Bing Ads Credits 2024-01-16 2024-01-17 117.4385736029571600
Margaret Hamilton Microsoft Mouse Surface Pro 2024-01-21 2024-01-27 3747.5952289342054600
Margaret Hamilton Microsoft Keyboard Microsoft Office 365 Subscription 2024-01-28 2024-01-31 117.4168297455968700
Paul Allen Halo Infinite Game Microsoft Office 365 Subscription 2024-01-12 2024-01-14 66.67777962993832305400
Paul Allen Halo Infinite Game Microsoft Office 365 Subscription 2024-01-24 2024-01-24 66.67777962993832305400
Ray Ozzie Bing Ads Credits Xbox Series X 2024-01-12 2024-01-19 399.9900000000000000
Ray Ozzie Xbox Series X Surface Pro 2024-01-19 2024-01-20 100.0020000400008000
Satya Nadella LinkedIn Premium Subscription Microsoft Office 365 Subscription 2024-01-01 2024-01-03 233.4111370456818900
Satya Nadella LinkedIn Premium Subscription Windows 11 License 2024-01-13 2024-01-15 366.7889296432144000
Satya Nadella Windows 11 License Visual Studio Professional 2024-01-15 2024-01-22 256.4540324308879200
Satya Nadella LinkedIn Premium Subscription Windows 11 License 2024-01-25 2024-01-26 366.7889296432144000
Satya Nadella Windows 11 License Visual Studio Professional 2024-01-26 2024-01-27 256.4540324308879200
Satya Nadella Windows 11 License Visual Studio Professional 2024-01-26 2024-01-27 256.4540324308879200
Steve Ballmer Microsoft Mouse Windows 11 License 2024-01-02 2024-01-04 438.6302424009234300
Steve Ballmer Microsoft Mouse Visual Studio Professional 2024-01-14 2024-01-16 1819.9692189303578300
Steve Ballmer Microsoft Mouse Visual Studio Professional 2024-01-14 2024-01-16 1819.9692189303578300
Steve Ballmer Microsoft Teams Headset Visual Studio Professional 2024-01-28 2024-01-28 454.5060562284698300
Steve Ballmer Microsoft Mouse Microsoft Teams Headset 2024-01-26 2024-01-28 246.2485571373605200
Tim Berners-Lee Microsoft Mouse Microsoft Keyboard 2024-01-15 2024-01-15 76.95267410542516352400
Tim Berners-Lee Microsoft Mouse Microsoft Keyboard 2024-01-15 2024-01-15 76.95267410542516352400
Tim Berners-Lee LinkedIn Premium Subscription Xbox Series X 2024-01-15 2024-01-18 1567.1890630210070000
Tim Berners-Lee Microsoft Mouse Xbox Series X 2024-01-27 2024-01-30 1823.7783762985763800

This question is still open. Write your answer.