I would like to check if a day is missing during the current month in my result set.The table looks like this.
date balance
2017-06-01 404.66
2017-06-03 392.41
2017-06-07 295.41
2017-06-10 275.41
2017-06-13 155.24
2017-06-15 151.74
2017-06-20 11.15
2017-06-21 -5.15
Since the result set is always within a month, only the difference in days is needed. I need to check the following two cases.
-
if the difference between dates is 1, then take the corresponding balance as it is,
-
if the date difference is bigger than 1, then multiply the number of day difference by the previous balance.
EDIT: For example, the day difference between the first two rows is 2. My desired output is 2*404,66.
And then, the day difference between 2nd and 3rd row is 4. So 4*392.41.
Is there any short and efficient way of doing this?
I need to analyze more than 300.000 data.
Best Answer
I've used LAG function to get previous date and previous balance and a CASE statement to calculate the value.
dbfiddle here