我正在创建一个报告,其中我希望获取按天和标记分组的事件数,按特定标记(如'%sample%')和特定时间段(例如过去一周)进行过滤。 / p>
我正在使用SQL Server 2008。
有几天没有发生具有特定标记的事件。我遇到的问题是如何生成没有行的天数,并给它们一个零值。类似于以下内容:
Tag Date Count
=================== =============== ====================
Sample 2013-07-07 0
Sample 2013-07-08 0
Sample 2013-07-09 0
Sample 2013-07-10 0
Sample 2013-07-11 0
Sample 2013-07-12 1
Sample 2013-07-13 0
xxx Sample xxx 2013-07-07 0
xxx Sample xxx 2013-07-08 0
xxx Sample xxx 2013-07-09 0
xxx Sample xxx 2013-07-10 3
xxx Sample xxx 2013-07-11 0
xxx Sample xxx 2013-07-12 0
xxx Sample xxx 2013-07-13 0
yyy Sample yyy 2013-07-07 0
yyy Sample yyy 2013-07-08 0
yyy Sample yyy 2013-07-09 0
yyy Sample yyy 2013-07-10 1
yyy Sample yyy 2013-07-11 0
yyy Sample yyy 2013-07-12 0
yyy Sample yyy 2013-07-13 0
为了在图形中呈现数据,零日是很重要的,其中每个“标记”是它自己的图形,其中时间是X轴或计数是Y轴。
Tags表格如下:
CREATE TABLE Tags
(
[Id] [int] IDENTITY(1,1) NOT NULL,
[Name] [nvarchar](64) NOT NULL,
CONSTRAINT [PK_Tags] PRIMARY KEY CLUSTERED
(
[Id] ASC
) ON [PRIMARY]
) ON [PRIMARY]
事件表如下所示:
CREATE TABLE [dbo].[Events](
[Id] [int] IDENTITY(1,1) NOT NULL,
[Message] [varchar](128) NULL,
[TagId] [int] NOT NULL,
[CreatedAt] [datetime] NULL,
CONSTRAINT [PK_Events] PRIMARY KEY CLUSTERED
(
[Id] ASC
) ON [PRIMARY]
)
其中TagId是Tags表的外键。
“事件”表包含以下数据
Id Message TagId CreatedAt
=== =========== ======= =========================
1 Message 1 1 2013-07-10 18:46:04.967
2 Message 2 2 2013-07-14 18:46:10.547
3 Message 3 3 2013-07-12 18:46:15.190
4 Message 4 4 2013-07-14 18:46:20.673
5 Message 5 2 2013-07-14 18:46:28.133
8 Message 6 1 2013-07-10 14:46:04.967
9 Message 7 1 2013-07-10 12:46:04.967
10 Message 6 2 2013-07-10 14:46:04.967
标签表包含以下数据:
Id Name
=== ===========================
3 Sample
4 Test1
5 Test2
6 Test3
1 xxx Sample xxx
2 yyy Sample yyy
所以,我用一张表加入了它:
SELECT Tags.Name, CONVERT(date, Events.CreatedAt) AS Date,COUNT(*) AS Count
FROM
Events
INNER JOIN Tags ON Events.TagId = Tags.Id
where tags.Name like '%sample%'
GROUP BY Tags.Name, CONVERT(date, Events.CreatedAt)
ORDER BY Tags.Name, CONVERT(date, Events.CreatedAt)
返回
Name Date Count
=================== =============== ================
Sample 2013-07-12 1
xxx Sample xxx 2013-07-10 3
yyy Sample yyy 2013-07-10 1
yyy Sample yyy 2013-07-14 2
我搜索了生成没有数据的天数的方法。我找到了一个条目 SQL Server: How to select all days in a date range even if no data exists for some days但无法让它发挥作用。
为了验证我到了正确的日子,我运行了以下查询:
WITH DateTable
AS
(
SELECT CONVERT(date, DateAdd(WEEK, -1, GETDATE())) AS [DATE]
UNION ALL
SELECT DATEADD(dd, 1, [DATE])
FROM DateTable
WHERE DATEADD(dd, 1, [DATE]) < CONVERT(date, GETDATE())
)
select DateTable.DATE
FROM DateTable
返回了:
2013-07-07
2013-07-08
2013-07-09
2013-07-10
2013-07-11
2013-07-12
2013-07-13
我的第一次尝试是在没有在where子句中指定LIKE '%sample%'
的情况下使其工作。
WITH DateTable
AS
(
SELECT CONVERT(date, DateAdd(WEEK, -1, GETDATE())) AS [DATE]
UNION ALL
SELECT DATEADD(dd, 1, [DATE])
FROM DateTable
WHERE DATEADD(dd, 1, [DATE]) < CONVERT(date, GETDATE())
)
SELECT Tags.Name, dt.[DATE] as Date, COUNT(Events.ID) as Count
FROM
Events
INNER JOIN Tags ON Tags.Id = Events.TagId
RIGHT JOIN [DateTable] dt ON dt.[DATE] = CONVERT(date, Events.[CreatedAt])
WHERE TagId IS NOT NULL
GROUP BY Tags.Name, dt.[DATE]
我得到以下结果:
Name Date Count
=================== =============== ================
xxx Sample xxx 2013-07-10 3
yyy Sample yyy 2013-07-10 1
Sample 2013-07-12 1
我尝试了其他方法,例如将RIGHT JOIN
更改为LEFT JOIN
,但我无法获得所需的结果。
答案 0 :(得分:2)
WITH DateTable
AS
(
SELECT CONVERT(date, DateAdd(WEEK, -1, GETDATE())) AS [DATE]
UNION ALL
SELECT DATEADD(dd, 1, [DATE])
FROM DateTable
WHERE DATEADD(dd, 1, [DATE]) < CONVERT(date, GETDATE())
)
SELECT Tags.Name, dt.DATE as Date,COUNT(Events.ID) as Count
FROM DateTable dt
CROSS JOIN Tags Tags
LEFT JOIN Events ON dt.[DATE] = CONVERT(date, Events.[CreatedAt])
and Tags.Id = Events.TagId
where tags.Name like '%sample%'
GROUP BY Tags.Name, Date
ORDER BY Tags.Name, Date;