sql server 按日期时间截止点 10:00am 进行分组
我正在尝试编写一个 Microsoft SQL Server 2005 查询来计算总值,按日期分组,截止点为上午 10:00?
例如:Table Orders
DateReceived Total
01-01-2012 06:10:01 2
01-01-2012 08:10:01 2
01-01-2012 10:10:01 4
02-01-2012 08:00:07 4
02-01-2012 10:00:07 4
我想计算每日总数,使用上午 10:00 作为截止点,因此上午 10:00 之前的任何订单都会出现在前一天的总数中,上午 10:00 之后的订单会出现在前一天的总数中。当天总计。
我希望看到如下查询结果:
DateReceived Total
31-12-2011 4
01-01-2012 8
02-01-2012 4
我知道如何仅按 Microsoft SQL Server 中的日期进行分组:
SELECT DISTINCT CONVERT(varchar, [DateReceived], 111) AS [dt_DateReceived],
SUM([Total]) AS perday
FROM [Orders]
GROUP BY CONVERT(varchar, [DateReceived], 111)
ORDER BY [DateReceived] DESC
但是我不确定如何使用 Microsoft SQL Server 添加上午 10:00 的截止时间。
使用 MySQL,我可以通过按减去的间隔进行分组来实现此目的,但是我不确定如何将其转换为 SQL Server:
GROUP BY
DATE(DATE_SUB( DateReceived , INTERVAL 10 HOUR))
有人可以建议吗?
谢谢你, 杰克
I'm trying to write a Microsoft SQL Server 2005 query that counts a total value, grouped by date with a cut-off point of 10:00am ?
eg: Table Orders
DateReceived Total
01-01-2012 06:10:01 2
01-01-2012 08:10:01 2
01-01-2012 10:10:01 4
02-01-2012 08:00:07 4
02-01-2012 10:00:07 4
I'd like to count the daily total, using 10:00 am as the cut-off point, so any orders before 10:00am appear in the total for the day before, and after 10:00 am in the total for that day.
I'm hoping to see query results like:
DateReceived Total
31-12-2011 4
01-01-2012 8
02-01-2012 4
I know how to group by just the date in Microsoft SQL Server:
SELECT DISTINCT CONVERT(varchar, [DateReceived], 111) AS [dt_DateReceived],
SUM([Total]) AS perday
FROM [Orders]
GROUP BY CONVERT(varchar, [DateReceived], 111)
ORDER BY [DateReceived] DESC
However I am unsure how to add a cut off time of 10:00am using Microsoft SQL Server.
Using MySQL, I can achieve this by grouping on a subtracted interval, however am unsure how to translate this to SQL Server:
GROUP BY
DATE(DATE_SUB( DateReceived , INTERVAL 10 HOUR))
Could anyone advise?
Thank you,
Jack
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
参见翻译:
测试脚本
注意我本地的日期时间格式是yyyy-mm-dd
测试脚本可以执行这里
PS:不需要区分
See the translation:
Test script
Note that my local datetime format is yyyy-mm-dd
The testscript can be executed here
PS: Distinction is not needed