SQL Challenge, getting historical data
Here is an interesting problem that I had to face at work. I need to get historical data from this table. Basically, I need to count the number of opened bugs per months.
Id OpenedAt ClosedAt
--- ---------- -----------
1 2007-04-02 2007-09-01
2 2007-07-01 NULL
3 2007-07-01 2007-07-23
A solution to this with the data above should print (for 2007-04 to 2007-10):
Month | Year | Opened Bugs |
4 | 2007 | 1 |
5 | 2007 | 1 |
6 | 2007 | 1 |
7 | 2007 | 2 |
8 | 2007 | 2 |
9 | 2007 | 1 |
10 | 2007 | 1 |
Have fun :-)
Comments
Is this what you want?
SELECT MONTH(OpenedAt) AS 'Month',
FROM Bugs
GROUP BY
@Mark:
I think yours is calculation how many were OPENED during a month, not how many were OPEN.
@Ayende:
By the example you gave, it seams you want to count bugs that were open at the end of the month (so the third bug isn't calculated into July)
Here's a crude stored proc to create a yearly report with that logic.
CREATE FUNCTION dbo.GetMonthStart (@Year AS INT, @Month AS INT)
RETURNS DATETIME
AS
BEGIN
END
CREATE FUNCTION dbo.GetNextMonthStart (@Year AS INT, @Month AS INT)
RETURNS DATETIME
AS
BEGIN
END
CREATE PROCEDURE dbo.YearlyBugStatus (@Year AS INT)
AS
BEGIN
END
EXEC dbo.YearlyBugStatus 2007
As I said, it's crude. But works
The temp table is needed for "empty" months, and for keeping the NextMonthStart computation for once per month.
@Ken.
That's exactly right. But I read: "the number of opened bugs". So I was thinking about how many were opened during a month. Not the bugs that are still open.
The following is a bit of a hack, as you really need a UDF that'll generate the first day of each month that needs reporting. However, the idea is the same. That being said, I suspect that a cursor might well be faster.
with Data as (
)
select seq month,
from (
) t
Assuming you're okay with NOT seeing months where there are no bugs:
SELECT
FROM
GROUP BY
ORDER BY
declare @StartDate datetime
declare @EndDate datetime
declare @MaxDate datetime
set @StartDate = '2007-04-01'
set @EndDate = '2007-10-01'
set @MaxDate = '2999-12-31'
declare @t table (Year int, Month int)
-- build table for reporting date range
declare @d datetime
set @d = @StartDate
while @d <= @EndDate
begin
end
-- create aggreate of open bugs per month/year
select
from
group by
Ken,
Good catch, I want all bugs opened and closed in a month, so my example data is actually having a bug :-)
Here's my stab at it. If I "interpret" what I think your business case is, I think the sample result is off.
Here's the total of all open bugs at the end of a month (excludes bugs that are closed in the same month) which is what Ken is deriving:
WITH cal AS (SELECT CAST('2007-4-01' AS datetime) AS DateValue
Results
Month Year OpenedBugs
4 2007 1
5 2007 1
6 2007 1
7 2007 2
8 2007 2
9 2007 1
10 2007 1
11 2007 1
12 2007 1
This shows all number of bugs that were open at some time during the month:
WITH cal AS (SELECT CAST('2007-04-01' AS datetime) AS DateValue
Results
Month Year OpenedBugs
4 2007 1
5 2007 1
6 2007 1
7 2007 3
8 2007 2
9 2007 2
10 2007 1
11 2007 1
12 2007 1
Isn't SQL cool?
Comment preview