Hi I have a data table that looks like this
| OrderID | Date | Product Name | CustomerID |
|---|---|---|---|
| 476070 | February 24,2021 | WARRANTY CLAIM | ABC123 |
| 476069 | February 22,2021 | PRODUCT C | ABC123 |
| 476068 | February 10,2021 | WARRANTY CLAIM | ABC123 |
| 476067 | February 1,2021 | WARRANTY CLAIM | ABC123 |
| 476064 | January 23,2021 | PRODUCT B | ABC123 |
| 476051 | January 10,2021 | WARRANTY CLAIM | ABC123 |
| 475023 | January 1 , 2021 | PRODUCT A | ABC123 |
What I am trying to achieve is for each warranty claim , I want to get the last product purchase of that customer.
So meaning that the output I want to achieve is like this
| OrderID | Date | Product Name | CustomerID | LASTPRODUCTPURCHASE |
|---|---|---|---|---|
| 476070 | February 24,2021 | WARRANTY CLAIM | ABC123 | PRODUCT C |
| 476069 | February 22,2021 | PRODUCT C | ABC123 | NULL |
| 476068 | February 10,2021 | WARRANTY CLAIM | ABC123 | PRODUCT B |
| 476067 | February 1,2021 | WARRANTY CLAIM | ABC123 | PRODUCT B |
| 476064 | January 23,2021 | PRODUCT B | ABC123 | NULL |
| 476051 | January 10,2021 | WARRANTY CLAIM | ABC123 | PRODUCT A |
| 475023 | January 1 , 2021 | PRODUCT A | ABC123 | NULL |
I have tried LAG and MAXIF but it returns either the previous order or the latest order for that particular customer. I need to have the LAST product purchase of that customer and only applicable for warranty claim orders.
I am getting this output for the CTE solution
This is the query I ran :
WITH cte AS (
SELECT *, MAX(CASE WHEN productName <> 'Warranty Service' OR productName <> 'Warranty Collect' OR productName <> 'Warranty Inspection' OR productName <> 'Delivery' THEN id END) OVER
(PARTITION BY vpn ORDER BY ordercompleted_at) MaxOrderID
FROM baterikudatawarehouse.bateriku_production.BaterikuOrderTrafficJune2020
where orderStatus = 'completed' and vpn = 'QMY70' order by ordercompleted_at DESC
)
SELECT t1.id ,t1.ordercompleted_at, t1.productName, t1.vpn,
CASE WHEN t1.productName = 'Warranty Service' OR t1.productName = 'Warranty Collect' OR t1.productName = 'Delivery' OR t1.productName = 'Warranty Inspection'
THEN t2.productName END AS LASTPRODUCTPURCHASE
FROM cte t1
INNER JOIN baterikudatawarehouse.bateriku_production.BaterikuOrderTrafficJune2020 t2
ON t2.vpn = t1.vpn AND
t2.id = t1.MaxOrderId ORDER BY t1.ordercompleted_at DESC;
