I need to get the 1st and last day of the last full 6 months ie, Feb,1,2012 and Feb 28,2012 (or 29 if leap year) Jan 1, 2012 - Jan 31,2012 - Dec 1, 2011 - Dec 31,2011 - Nov 1,2011 - Nov 30,2011 and so on. I need that series in separate fields in order to use them in a query to gather the prior data from the table.
The table I am querying is storing material use, and each month use needs to be summed and added to the sum of all prior months. So the above vales would be use in the select as (this example uses November, I will need to use the same thing in the query to grab 6 months of data)
SELECT SUM(CASE WHEN movedate <= '2010-11-01'
THEN (booked+adjusts)-(used+spoiled) ELSE 0 END) as qBegin,
SUM(CASE WHEN movedate between '2010-11-01' and '2010-11-30'
THEN (booked+adjusts)-(used+spoiled) ELSE 0 END) as qEnd
That will give me the beginning value and current month value, which I need to do for 6 months. The query could be run anytime during a month, but would only want to return the past 6 full months - if run on March 15th, it would exclude March and only show Feb of the current year back to September of the prior year. Any suggestions?
The table I am querying is storing material use, and each month use needs to be summed and added to the sum of all prior months. So the above vales would be use in the select as (this example uses November, I will need to use the same thing in the query to grab 6 months of data)
SELECT SUM(CASE WHEN movedate <= '2010-11-01'
THEN (booked+adjusts)-(used+spoiled) ELSE 0 END) as qBegin,
SUM(CASE WHEN movedate between '2010-11-01' and '2010-11-30'
THEN (booked+adjusts)-(used+spoiled) ELSE 0 END) as qEnd
That will give me the beginning value and current month value, which I need to do for 6 months. The query could be run anytime during a month, but would only want to return the past 6 full months - if run on March 15th, it would exclude March and only show Feb of the current year back to September of the prior year. Any suggestions?