My Source table looks like below
Sales Date Sales Value
4/1/2019 20
5/1/2019 50
6/1/2019 60
7/1/2019 10
8/1/2019 20
9/1/2019 10
4/1/2020 70
5/1/2020 100
6/1/2020 10
7/1/2020 10
8/1/2020 20
9/1/2020 20
Output should be:
Year Month Sales_Value_Last_Month_of_each_year/Month Last_Year_Same_Month
2019 4 20
2019 5 50 20
2019 6 60 50
2019 7 10 60
2019 8 20 10
2019 9 10 20
2020 4 70 0 20
2020 5 100 70 50
2020 6 10 100 60
2020 7 10 10 10
2020 8 20 10 20
2020 9 20 20 10
Explanation:
for 2020 and 05 Month, Sales_Value_Last_Month_of_each_year/Month should be 70 as this is the previous month value(2020 and 04 is 70) and if the previous value is not exist, it should be 0. For for 2020 and 05 Month, Last_Year_Same_Month should be 50 as last year 2019 and 05 is 50 and if the values is not exist for corresponding month of last year, it should be 0.
I cannot go for regular last month data like add_months(sales_date, 'month')-1 in where condition, since my output is to show everything like each_month_sales_value, Sales_Value_Last_Month_of_each_year/Month and Last_Year_Same_Month
I am not able find the way to write this in oracle sql. Could someone please suggest a way to achieve this result