0

如果可能的话,我想在一份声明中做到这一点。

我原来的查询是这样的:

SELECT CAST(Date as DATE) as DayDate, SUM(Books) as LostBooks 
FROM RestartBooksView 
WHERE Name = 'AUHT167' and Date > (getdate() - 105) 
GROUP BY CAST(Date as DATE) 
ORDER BY CAST(Date as DATE) ASC

这个查询的问题是我希望总和反映在上午 7 点到 7 点,而不是当前的 12 点到 12 点。如何修改我的查询以反映此更改?

4

1 回答 1

3

假设“日期”列实际上是一个日期时间。

SELECT CAST(Date as DATE) as DayDate, SUM(Books) as LostBooks
FROM (select rbv.*,
             cast(dateadd(h, -7, date) as date) as newdate
      from RestartBooksView rbv
     )
WHERE Name = 'AUHT167' and newdate > (getdate() - 105)
GROUP BY newdate
ORDER BY CAST(Date as DATE) ASC

我还使用了 SQL Server 日期函数,因为您似乎正在使用该数据库。

于 2012-07-23T15:24:45.183 回答