I'm using the below query to retreive data from a table, the current day and the previous day and I'm doing a calculation v_calc as mentioned below.

The first loop gets the data for the current day and the inner loop gets the data for the previous day. Is there a better way to write this query? Also, if there is no record found in the inner query, I'd like to substitute the values with the current. How do I do this? Many thans for your help.