SQL查询未返回预期的日期时间范围

时间:2014-10-13 16:15:31

标签: sql sql-server

我正在处理一个查询,希望能够在特定日期的特定时间范围内查询数据库。如果我查询一整天的数据,我会得到正确的数据。每小时可获得一行数据(0 - 23)。

WHERE Documents.CreationTime BETWEEN '2014-10-01 00:00:00.000' AND '2014-10-01 23:59:59.999'

如果我尝试查询当天的部分时间,结果就不常见了。

WHERE Documents.CreationTime BETWEEN '2014-10-01 00:00:00.000' AND '2014-10-01 06:00:00.000'

部分日查询返回:(注意小时数从0跳到19)

    Hours   Faxes   Good Page Count
    0         3       4
    19       15      58
    20        4       9
    21        8      42
    22        2       4
    23        4      12

这是我为尝试解决问题而创建的简化查询。

SELECT DATEPART(hour, DATEADD(HH, - DATEDIFF(Hour, GETDATE(), GETUTCDATE()), Documents.CreationTime)) AS Hours
    ,COUNT(*) AS Faxes
    ,SUM(goodpagecount) AS [Good Page Count]
FROM Documents
JOIN Users
    ON Documents.OwnerID = Users.handle
JOIN Groups
    ON Users.GroupID = Groups.handle
JOIN History
    ON History.OWNER = Documents.handle
JOIN HistoryTRX
    ON History.handle = HistoryTRX.handle
WHERE Documents.CreationTime BETWEEN '2014-10-01 00:00:00.000'
        AND '2014-10-01 06:00:00.000'
GROUP BY DATEPART(hour, DATEADD(HH, - DATEDIFF(Hour, GETDATE(), GETUTCDATE()), Documents.CreationTime))
ORDER BY DATEPART(hour, DATEADD(HH, - DATEDIFF(Hour, GETDATE(), GETUTCDATE()), Documents.CreationTime))

有关我缺少什么或改进的建议吗?

编辑 - 更多细节 " Documents.CreationTime"是UTC。我期待着#34;小时"列对应于当地时间。在这种情况下,UTC -5作为此条目。

2 个答案:

答案 0 :(得分:1)

如何在where子句中使用DATEADD函数:

WHERE Documents.CreationTime >= '20141001' AND Documents.CreationTime <= DATEADD(HOUR,6,'20141001')

由Aaron Bertrand撰写的Lamak评论的有趣博客:

答案 1 :(得分:0)

根据回答我的问题提供的建议,我想出了以下新查询:

SELECT  DATEPART(hour, DATEADD(HH,-DATEDIFF(Hour,GETDATE(),GETUTCDATE()),Documents.CreationTime)) AS Hours ,COUNT(*) AS Faxes,SUM(goodpagecount) AS [Good Page Count]
FROM Documents
JOIN Users ON Documents.OwnerID=Users.handle
JOIN Groups ON Users.GroupID=Groups.handle
JOIN History ON History.Owner=Documents.handle
JOIN HistoryTRX ON History.handle=HistoryTRX.handle
WHERE DATEADD(HH,-DATEDIFF(Hour,GETDATE(),GETUTCDATE()),Documents.CreationTime) >= '2014-10-01 00:00:00.000' and DATEADD(HH,-DATEDIFF(Hour,GETDATE(),GETUTCDATE()),Documents.CreationTime) <= '2014-10-03 08:00:00.000'
GROUP BY  DATEPART(hour, DATEADD(HH,-DATEDIFF(Hour,GETDATE(),GETUTCDATE()),Documents.CreationTime))
ORDER BY  DATEPART(hour, DATEADD(HH,-DATEDIFF(Hour,GETDATE(),GETUTCDATE()),Documents.CreationTime))

我的更改是针对&#34; WHERE&#34;通过添加我的UTC补偿声明。 &#34; WHERE&#34;现在匹配&#34; SELECT&#34;。

在:

WHERE Documents.CreationTime >= '2014-10-01 00:00:00.000' and Documents.CreationTime <= '2014-10-03 08:00:00.000'

后:

WHERE DATEADD(HH,-DATEDIFF(Hour,GETDATE(),GETUTCDATE()),Documents.CreationTime) >= '2014-10-01 00:00:00.000' and DATEADD(HH,-DATEDIFF(Hour,GETDATE(),GETUTCDATE()),Documents.CreationTime) <= '2014-10-03 08:00:00.000'

同时删除了BETWEEN关键字,因为它可能不如我想要的那样精确。

结果现在看起来像:

    Hours   Faxes   Good Page Count
      0       3      4
      1       5      9
      3       9     50
      4       8     16
      5      14     40