我需要两次之间的时间差,以分钟为单位,我希望结果按差值和计数进行分组

I need the time difference between two times in minutes, and i want the result to be grouped by the difference and number of count

我有一个名为 Order_Timing 的 table,其中包含 2 个名为 StartTime 和 EndTime 的列,如下所示。我想得到分钟和计数的差异。

start time End Time
2022-03-14 09:28:42.250 2022-03-14 09:29:23.693
2022-03-14 09:28:42.250 2022-03-14 09:30:40.150
2022-03-14 09:37:59.577 2022-03-14 09:38:04.730
2022-03-14 09:38:26.097 2022-03-14 09:38:38.583

以下是我的预期结果

Minutes Count
0 - 1 mins 1
1 - 2 mins 2
2 - 3 mins 1

您需要通过对查询中获得的分差值进行分组来查找计数。

SELECT CASE 
        WHEN minute_diff = 0
            THEN '0-1 min'
        WHEN minute_diff = 1
            THEN '1-2 min'
        WHEN minute_diff = 2
            THEN '2-3 min'
        END AS Minutes
    ,count(*) AS [count]
FROM (
    SELECT [start time]
        ,[end time]
        ,DATEDIFF(MINUTE, [start time], [end time]) AS minute_diff
    FROM Order_Timing
    ) a
GROUP BY minute_diff
ORDER BY 1

您可以根据需要调整 case 语句。

select count(diff) 
from (select datediff(minute,start,enddate) as diff from [table])a 
group by diff