【发布时间】:2019-12-05 02:55:09
【问题描述】:
我目前有一个问题,即我在开始日期和结束日期之间的几分钟内执行 DATEDIFF,但是当这个日期进入新的月份时,我需要将每个月的数字分开。
请查看示例数据(文本和图像视图);
SELECT [BookingNum]
,[StartDate]
,[EndDate]
,[Location]
,DATEPART(m,startdate) AS [Month]
,DATEDIFF(MINUTE,StartDate,EndDate) AS [Minutes]
FROM [Test].[dbo].[Booking]
BookingNum StartDate EndDate Location Month Minutes
1 2019-02-05 12:54:00.000 2019-02-08 15:00:00.000 Area 1 2 4446
2 2019-05-02 10:41:00.000 2019-05-10 12:39:00.000 Area 2 5 11638
3 2019-06-01 10:30:00.000 2019-06-04 09:25:00.000 Area 3 6 4255
4 2019-02-02 09:41:00.000 2019-04-20 11:54:00.000 Area 1 2 111013
5 2019-03-29 19:09:00.000 2019-04-02 10:41:00.000 Area 3 3 5252
对于第 4 行和第 5 行,当它们跨越多个月时,需要额外的行。
我想看看第 4 行中的数据示例;
StartDate EndDate Location Month Minutes
2019-02-02 09:41:00.000 2019-02-28 23:59:00.000 Area 1 2 38298
2019-03-01 00:00:00.000 2019-03-31 23:59:00.000 Area 1 3 44639
2019-04-01 00:00:00.000 2019-04-20 23:59:00.000 Area 1 4 28074
这将给我该月的总分钟数,仅在开始日期和结束日期之间。
非常感谢任何帮助。
【问题讨论】:
-
用您正在使用的数据库标记问题。您当前的代码也很有帮助。
-
请标记您的 DBMS(MySQL、SQL Server 等)。另外,最好不要链接到图片,纯文本是好的。
-
谢谢,我添加了 SQL Server。
标签: sql sql-server datediff