I have a table which lists the Departments and total count of incidents for each calendar month, as seen below. Data is ordered by Dept and Date fields.
What I am trying to do is to get the minimum value of the 'Count' field for the past 3 months, for each company and display it for each row in the original table.
For instance, the first record has Count=2 and there is no data prior to 2011-1-1 for Dept-001, so the min(Count) is 2. The second record has Count=9 and the min(Count) for the past 3 months is 2. and so forth...
How would you write a query to give this output? Thanks,
TABLE
Dept Count Date
001 2 2011-1-1
001 9 2011-2-1
001 1 2011-3-1
001 4 2011-4-1
001 5 2011-5-1
001 3 2011-6-1
002 2 2011-1-1
002 7 2011-2-1
002 1 2011-3-1
002 9 2011-4-1
002 6 2011-5-1
002 1 2011-6-1
OUTPUT
Dept Min(Last 3 Month) Date
001 2 2011-1-1
001 2 2011-2-1
001 1 2011-3-1
001 1 2011-4-1
001 1 2011-5-1
001 3 2011-6-1
002 2 2011-1-1
002 2 2011-2-1
002 1 2011-3-1
002 1 2011-4-1
002 1 2011-5-1
002 1 2011-6-1