I currently have a table Bugs
that looks like this:
|ID |Priority |Created |Updated |Status |Category
|X123 |Major |01/01 |01/03 |Open |A
|X145 |Normal |01/01 |01/02 |Closed |B
|X678 |Minor |01/03 |01/03 |Open |C
|X763 |Major |01/02 |01/03 |Closed |C
All columns are varchar(25) except Created and Updated, which are dates.
I need to create a view with the following format:
|Date |Major |Normal |Minor |Category
|01/01 |4 |3 |4 |A
|01/01 |3 |5 |2 |B
|01/01 |2 |4 |7 |C
|01/02 |7 |3 |4 |A
|01/02 |3 |9 |5 |B
|01/02 |1 |6 |3 |C
Where the numbers under Major, Normal, and Minor are the count of CURRENTLY OPEN bugs of that priority on a given date. By currently open, I mean this: open bugs are active on the interval Created
-GETDATE()
, closed bugs are active on the interval Created
-Updated
.
I have a list of all the dates I need through this query:
WITH D AS
(
SELECT Dates AS DateValue
FROM DatesTable
WHERE Dates >= '2012-03-23'
AND Dates <= GETDATE()
),
Any ideas of how I might do this? I've played with the idea of a pivot query and grouping, but I've not been able to cover everything I need. Your help is greatly appreciated!
EDIT: The Bugs
table with example data
CREATE TABLE [dbo].[Bugs](
[ID] [varchar](25) NOT NULL,
[Priority] [varchar](25) NOT NULL,
[Updated] [date] NOT NULL,
[Created] [date] NOT NULL,
[Status] [varchar](25) NOT NULL,
[Category] [varchar](25) NOT NULL,
) ON [PRIMARY]
INSERT INTO Bugs VALUES (X123, Major, 01/01/12, 01/03/12, Open, A)
INSERT INTO Bugs VALUES (X145, Normal, 01/01/12, 01/02/12, Closed, B)
INSERT INTO Bugs VALUES (X678, Minor, 01/03/12, 01/03/12, Open, C)
INSERT INTO Bugs VALUES (X763, Major, 01/02/12, 01/03/12, Closed, C )