As shown in the image:
Here I need to perform cumulative based on a condition:
I have
Column 1: Sales Date
Column 2: Warranty End Date
Column 3: Qty of Sales
Column 4: Cumulative Sales Qty
Now for Column 5: Under warranty which gives a wrong value here when the warranty dates complete 365days(expires) I need to subtract the sales happened for that expiry date in the under warranty count column similar the count starts reducing for the following days when the warranty expires.
For eg in the figure: JAN 1,2014 have 246 as cumulative sales when it reaches JAN 1 2015, I need to subtract that '246' from '96502' which is the cumulative sales till JAN 1 2015.So should be 96256
How can we achieve the logic in SQL for 'Under warranty count' column
My Query:
SELECT a."sales_year",
a."sales_billing_date",
a."warranty_end_date",
a."sum_val",
a."order_qty",
CASE
WHEN Sum(Days_between (a."warranty_end_date", a."sales_billing_date"))
< 365
THEN a."sum_val"
ELSE ( ( a."sum_val" ) - ( b."sum_val" ) )
END AS "Under_Warranty"
FROM "_SYS_BIC"."pal/cv_pal_tbf_sales" a,
"_SYS_BIC"."pal/cv_pal_tbf_sales" b
GROUP BY a."sales_year",
a."sales_billing_date",
a."warranty_end_date",
a."order_qty",
b."sum_val",
a."sum_val";