SQL Challenge, getting historical datasolution
As it turned out, this isn't that hard, all I needed to do was remember my trusty DateRange function, modify it to on months instead of days, and it was off to the races. This type of code does make my head hurt a tiny bit, it packs a lot into it.
DECLARE @start DATETIME,
@end DATETIME
SET @start = '2007-04-01'
set @end = '2007-10-01'
SELECT YEAR(Currentdate) Year,
MONTH(Currentdate) Month,
COUNT(Bug.Id) OpenedBugCount
FROM Dbo.MonthRange(@start,@end )
JOIN Bugs Bug
ON YEAR(Currentdate) >= YEAR(Bug.Openedat)
AND MONTH(Currentdate) >= MONTH(Bug.Openedat)
AND YEAR(Currentdate) <= YEAR(ISNULL(Bug.Closedat,Currentdate))
AND MONTH(Currentdate) < MONTH(ISNULL(Bug.Closedat,DateAdd(MONTH,1,Currentdate)))
GROUP BY YEAR(Currentdate),MONTH(Currentdate)
I would get it into NHibernate on Sunday, should be fun, it is about the ninth select is the super report... :-)
More posts in "SQL Challenge, getting historical data" series:
- (12 Aug 2007) Performance characteristics
- (10 Aug 2007) solution

Comments
Comment preview