SQL Server:按昨天早上9点到今天晚上9点分组

问题描述:

50 2016-07-29 04:40:44.000 
70 2016-07-29 04:40:33.000 
30 2016-07-29 03:50:56.000 
15 2016-07-28 00:03:16.000 
74 2016-07-28 23:38:18.000 
15 2016-07-28 23:30:11.000 
7 2016-07-28 23:22:37.000 
10 2016-07-28 21:02:19.000 

昨天和今天如何才能实现群组?SQL Server:按昨天早上9点到今天晚上9点分组

我必须看到的只有一行:

271 2016-07-28 

感谢您的任何想法。

为您的特定数据,你可以做:

select sum(col1), cast(max(col2) as date) 
from t; 

你可能需要一个where条款:

where col2 >= dateadd(hour, -15, cast(getdate() as date)) and 
     col2 < dateadd(hour, 21, (cast getdate() as date))