I am a newbee to SQL and need to write a query which seems to be complex.
I need to write a query for getting the returns from a table which looks like:
id Price_date price
1 1-1-2010 20
1 2-2-2010 21
1 7-2-2010 22
1 27-2-2010 23
1 3-3-2010 23
Here is what I need to select from the above table:-
- id,
- price_date(date of last price of month),
- return((last_price_of_month/last_price_of_previous_month) -1),
- last_date_for_return_calculation(first day of next month)
And the sample data would be like:-
id price_date return last_date_for_return_calculation
1 27-2-2010 (23/20 -1) 1-3-2011
Could somebody help me in solving this problem?
based on your requirement, it seems your date are in 2011, not 2010. Also I added a row in january to be sure to get the last row of the previous month of the sample data.