我目前表Bugs
,如下所示:
|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
所有列都是varchar(25),但Created和Updated除外,它们是日期。
我需要使用以下格式创建视图:
|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
其中,Major,Normal和Minor下的数字是指在给定日期当前优先级错误的计数。目前已开放,我的意思是:在Created
- GETDATE()
区间内,已开启的错误处于有效状态,已在Created
- Updated
区间内处于关闭错误状态。
我通过此查询我需要的所有日期的列表:
WITH D AS
(
SELECT Dates AS DateValue
FROM DatesTable
WHERE Dates >= '2012-03-23'
AND Dates <= GETDATE()
),
关于我如何做到这一点的任何想法?我玩过一个数据透视查询和分组的想法,但我无法涵盖我需要的一切。非常感谢您的帮助!
编辑:带有示例数据的Bugs
表
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 )
答案 0 :(得分:1)
这样的事情可能就是你想要的。
select b.Date
, SUM(case when b.Priority = 'Major' then 1 end) as Major
, SUM(case when b.Priority = 'Normal' then 1 end) as Normal
, SUM(case when b.Priority = 'Minor' then 1 end) as Minor
, b.Category
from Bugs b
group by b.Date, b.Category
order by b.Date
答案 1 :(得分:1)
我没有使用cte,而是使用查询作为你需要的基础。通过将datestable与您的bug表连接起来,我们可以比较bug表中的日期,看看bug是否在给定日期打开。我们知道d.dates必须大于bug的创建日期。我们还需要检查错误状态。如果错误是打开的,那么我们只想将错误包括在更新日期之前。否则,由于它仍处于打开状态,因此需要将bug包括在内。其余的基本上是手动旋转数据以获得所需的值。
SELECT d.Dates, sum(case when b.priority = major then 1 else 0 end) as major,
sum(case when b.priority = normal then 1 else 0 end) as normal,
sum(case when b.priority = minor then 1 else 0 end) as minor,
b.category
FROM DatesTable d
join bugs b
on d.dates >= b.Created
and case when b.status = 'closed' then b.updated else '12-31-9999' end <= d.dates
WHERE d.Dates >= '2012-03-23'
AND d.Dates <= GETDATE()
Group by d.dates,b.category
答案 2 :(得分:1)
这是我最后测试的内容:
SELECT d.Dates as Date,
SUM(CASE Priority WHEN 'Major' THEN 1 ELSE 0 END) as Major,
SUM(CASE Priority WHEN 'Normal' THEN 1 ELSE 0 END) as Normal,
SUM(CASE Priority WHEN 'Minor' THEN 1 ELSE 0 END) as Minor,
b.Category
FROM Bugs b
INNER JOIN DatesTable d ON d.Dates >= b.Created
WHERE (Status = 'Closed' AND d.Dates <= Updated OR Status = 'Open')
AND d.Dates <= GETDATE() AND d.Dates >= '2012-01-01'
GROUP BY d.Dates, b.Category
ORDER BY d.Dates
答案 3 :(得分:0)
您可以使用以下查询
SELECT DatesTable.Dates
, SUM(CASE WHEN bugs.Priority = 'Major' THEN 1 ELSE 0 END) AS Major
, SUM(CASE WHEN bugs.Priority = 'Normal' THEN 1 ELSE 0 END) AS Normal
, SUM(CASE WHEN bugs.Priority = 'Minor' THEN 1 ELSE 0 END) AS Minor
, bugs.Category
FROM DatesTable
LEFT JOIN Bugs ON DatesTable.Dates = Bugs.Date
-- You may need to cast this, if the data type do not match
AND Bugs.Status='Open'
WHERE Dates >= '2012-03-23'
AND Dates <= GETDATE()
GROUP BY DatesTable.Dates, bugs.Category
ORDER BY DatesTable.Dates
答案 4 :(得分:0)
我们可以使用嵌套查询来获取我们想要处理的数据,并使用父查询来显示最终结果:
SELECT CreatedDate AS [Date],
SUM(Major) AS Major,
SUM(Normal) AS Normal,
SUM(Minor) AS Minor,
Category
FROM (
SELECT DATEADD(dd, DATEDIFF(dd, 0, Created), 0) AS CreatedDate,
CASE WHEN Priority = 'Major' THEN 1 ELSE 0 END AS Major,
CASE WHEN Priority = 'Normal' THEN 1 ELSE 0 END AS Normal,
CASE WHEN Priority = 'Minor' THEN 1 ELSE 0 END AS Minor,
Category
FROM Bugs
WHERE Status = 'Open'
) AS t
GROUP BY CreatedDate, Category
ORDER BY CreatedDate, Category
(从DATETIME删除时间的信用转到this SO question )
嵌套查询可以更轻松地查看正在操作的数据,并根据需要过滤所述数据。
我假设日期是DATETIME,因此我删除了分组的时间部分。
请参阅此SQL Fiddle中的示例(我只为前两个条目添加了足够的数据以匹配预期结果)。