Window function (lead, lag) to sumup if the partition name is the same

Viewed 23

I need to get the previous and next row based on the partition by groupid, buid and packageid.

The issue is that a customer may have two packages within the same partition. What would be the best way to handle this? I cannot group because I will need that extra row later. Thanks

The end goal is to calculate the difference between the previous month and the current month as well as previous month and next month. This is needed to calculate the churn, reactivation and so on.

I need to either remove the previous and next columns and only look at the sum of the values based on the partition that are invoices date, groupid, buid and item_id. The query that I wrote below, should be based on the sum of these values, I highlighted in red.

date groupid buid item_id previous next amount
1/1 1 2 5 20 20
1/2 1 2 6 10 10
1/1 1 2 6 10 10
2/1 1 2 5 20 20 20
2/1 1 2 6 10 10 10
2/1 1 2 6 10 10
3/1 1 2 5 20 20
3/1 1 2 6 10 20

This is my query so far:

   SELECT
     *
   , "lag"(amount_usd, 1) OVER (PARTITION BY invoice_item_customer_id, invoice_item_related_customer_id, item_id ORDER BY "date_trunc"('month', date_invoice), package_name ASC) prev_amount
   , "lead"(amount_usd, 1) OVER (PARTITION BY invoice_item_customer_id, invoice_item_related_customer_id, item_id ORDER BY "date_trunc"('month', date_invoice), package_name ASC) next_amount
   , "lag"(date_invoice, 1) OVER (PARTITION BY invoice_item_customer_id, invoice_item_related_customer_id, item_id ORDER BY "date_trunc"('month', date_invoice), package_name ASC) prev_invoice_date
   , "lead"(date_invoice, 1) OVER (PARTITION BY invoice_item_customer_id, invoice_item_related_customer_id, item_id ORDER BY "date_trunc"('month', date_invoice), package_name ASC) next_invoice_date
   FROM
     group_cust
) 
, cte_status AS (
   SELECT
     *
   , (CASE WHEN (((amount_usd >= 0) AND (next_amount IS NULL)) AND ((date_invoice < "date_add"('month', -1, current_date)) OR ("date_trunc"('month', next_invoice_date) > "date_add"('month', 1, "date_trunc"('month', date_invoice))))) THEN 'churn_next_month' ELSE null END) churn
   , (CASE WHEN ((CAST("date_trunc"('month', first_invoice_date_package) AS date) = "date_trunc"('month', date_invoice)) AND (amount_usd >= 0)) THEN 'new' WHEN (((CAST("date_trunc"('month', first_invoice_date) AS date) < "date_trunc"('month', date_invoice)) AND (amount_usd >= 0)) AND ("date_trunc"('month', prev_invoice_date) < "date_add"('month', -1, "date_trunc"('month', date_invoice)))) THEN 'reactivation' WHEN ((first_invoice_date_buid > first_invoice_date) AND ("date_trunc"('month', date_invoice) = first_invoice_date_buid)) THEN 'group expansion' WHEN (((amount_usd >= 0) AND (prev_amount >= 0)) AND (amount_usd > prev_amount)) THEN 'upsell' WHEN (((amount_usd >= 0) AND (prev_amount >= 0)) AND (amount_usd < prev_amount)) THEN 'downsell'  WHEN (((amount_usd >= 0) AND (prev_amount IS NOT NULL)) AND (amount_usd = prev_amount)) THEN 'same' ELSE null END) donor_status
   FROM
     cte_period
) 
0 Answers
Related