每个日期的等待时间(带间隔计数)

时间:2019-02-18 04:15:37

标签: sql sql-server

基于模式和数据,我们需要找到多次输入的每辆车的等待时间和每个日期的停车次数。

   --- CREATE TABLE
    CREATE TABLE [dbo].[Table_Detail](
        [Sno] [int] NOT NULL,
        [VehicleId] [nchar](10) NULL,
        [DriverId] [nchar](10) NULL,
        [LocationId] [nchar](10) NULL,
        [StartTime] [datetime2](7) NULL,
        [EndTime] [datetime2](7) NULL
        )

    CREATE TABLE [dbo].[Table_Main](
        [Sno] [int] NOT NULL,
        [VehicleId] [nchar](10) NULL,
        [StartTime] [datetime2](7) NULL,
        [EndTime] [datetime2](7) NULL) 

         -- INSERT DATA

    INSERT [dbo].[Table_Main] ([Sno], [VehicleId], [StartTime], [EndTime]) VALUES (1, N'1001      ', CAST(N'2019-02-15T07:25:33.0000000' AS DateTime2), CAST(N'2019-02-15T17:25:33.0000000' AS DateTime2))
    INSERT [dbo].[Table_Main] ([Sno], [VehicleId], [StartTime], [EndTime]) VALUES (2, N'1002      ', CAST(N'2019-02-15T06:12:52.0000000' AS DateTime2), CAST(N'2019-02-15T11:21:35.0000000' AS DateTime2))
    INSERT [dbo].[Table_Main] ([Sno], [VehicleId], [StartTime], [EndTime]) VALUES (3, N'1003      ', CAST(N'2019-02-15T06:32:52.0000000' AS DateTime2), CAST(N'2019-02-15T11:21:35.0000000' AS DateTime2))
    INSERT [dbo].[Table_Main] ([Sno], [VehicleId], [StartTime], [EndTime]) VALUES (4, N'1003      ', CAST(N'2019-02-15T13:1:21.0000000' AS DateTime2), CAST(N'2019-02-15T19:23:32.0000000' AS DateTime2))
    INSERT [dbo].[Table_Detail] ([Sno], [VehicleId], [DriverId], [LocationId], [StartTime], [EndTime]) VALUES (1, N'1001      ', N'34        ', N'53        ', CAST(N'2019-02-15T07:55:32.0000000' AS DateTime2), CAST(N'2019-02-15T08:15:23.0000000' AS DateTime2))
    INSERT [dbo].[Table_Detail] ([Sno], [VehicleId], [DriverId], [LocationId], [StartTime], [EndTime]) VALUES (2, N'1002      ', N'23        ', N'65        ', CAST(N'2019-02-15T07:11:33.0000000' AS DateTime2), CAST(N'2019-02-15T07:45:33.0000000' AS DateTime2))
    INSERT [dbo].[Table_Detail] ([Sno], [VehicleId], [DriverId], [LocationId], [StartTime], [EndTime]) VALUES (3, N'1001      ', N'34        ', N'53        ', CAST(N'2019-02-15T09:22:52.0000000' AS DateTime2), CAST(N'2019-02-15T09:45:59.0000000' AS DateTime2))
    INSERT [dbo].[Table_Detail] ([Sno], [VehicleId], [DriverId], [LocationId], [StartTime], [EndTime]) VALUES (4, N'1002      ', N'23        ', N'65        ', CAST(N'2019-02-15T10:25:13.0000000' AS DateTime2), CAST(N'2019-02-15T11:15:23.0000000' AS DateTime2))
    INSERT [dbo].[Table_Detail] ([Sno], [VehicleId], [DriverId], [LocationId], [StartTime], [EndTime]) VALUES (5, N'1001      ', N'34        ', N'53        ', CAST(N'2019-02-15T11:25:36.0000000' AS DateTime2), CAST(N'2019-02-15T12:35:37.0000000' AS DateTime2))
    INSERT [dbo].[Table_Detail] ([Sno], [VehicleId], [DriverId], [LocationId], [StartTime], [EndTime]) VALUES (6, N'1001      ', N'34        ', N'53        ', CAST(N'2019-02-15T15:15:33.0000000' AS DateTime2), CAST(N'2019-02-15T15:25:21.0000000' AS DateTime2)) 
   INSERT [dbo].[Table_Detail] ([Sno], [VehicleId], [DriverId], [LocationId], [StartTime], [EndTime]) VALUES (7, N'1003      ', N'48       ', N'74   ', CAST(N'2019-02-15T07:13:13.0000000' AS DateTime2), CAST(N'2019-02-15T08:05:01.0000000' AS DateTime2))
   INSERT [dbo].[Table_Detail] ([Sno], [VehicleId], [DriverId], [LocationId], [StartTime], [EndTime]) VALUES (8, N'1003      ', N'48       ', N'74   ', CAST(N'2019-02-15T09:43:12.0000000' AS DateTime2), CAST(N'2019-02-15T10:05:42.0000000' AS DateTime2))
    INSERT [dbo].[Table_Detail] ([Sno], [VehicleId], [DriverId], [LocationId], [StartTime], [EndTime]) VALUES (9, N'1003      ', N'48       ', N'74   ', CAST(N'2019-02-15T14:13:13.0000000' AS DateTime2), CAST(N'2019-02-15T14:45:21.0000000' AS DateTime2))

每天的车辆运输等待时间和访问次数

VehicleId   ElapsedTime     NoOfRecords
1001        02:02:47:000    4
1002        01:24:10:000    2
1003        01:04:43:000    2        
1003        00:32:10:000    1

2 个答案:

答案 0 :(得分:0)

基本上,您将选择对table_detail中的vehicleid进行汇总的所有记录,其日期范围位于table_main中的记录的边界。

执行此操作的一种方法如下

select b.vehicleid
      ,dateadd(s,sum(datediff(s,b.starttime,b.endtime)),cast('20180101' as datetime)) as elapsed_time
      ,count(*)
  from dbo.table_main a
  join dbo.table_detail b 
    on a.vehicleid=b.vehicleid
   and a.starttime<=b.starttime
   and a.endtime>=b.endtime
group by b.vehicleid
        ,a.sno   

+-----------+-----------------------+-------------------+
| vehicleid |     elapsed_time      |    cnt_of_records |
+-----------+-----------------------+-------------------+
|      1001 |   01/01/2018 02:02:47 |                 4 |
|      1002 |   01/01/2018 01:24:10 |                 2 |
|      1003 |   01/01/2018 01:14:18 |                 2 |
|      1003 |   01/01/2018 00:32:08 |                 1 |
+-----------+-----------------------+-------------------+

这是dbfiddle链接

https://dbfiddle.uk/?rdbms=sqlserver_2017&fiddle=6410fb6be1b55f989445f0c61440e141

答案 1 :(得分:0)

您可以尝试以下操作。

SELECT vehicleid, 
       CONVERT(VARCHAR, Dateadd(ms, te, 0), 114) AS ElapsedTime, 
       ct                                        AS NoOfRecords 
FROM   (SELECT vehicleid, 
               Sno,
               Sum(dms) te, 
               Count(*) ct 
        FROM   (SELECT d.vehicleid, m.Sno,
                       Datediff(ms, d.starttime, d.endtime) dms 
                FROM   [table_detail] d
                inner join [Table_Main] m on d.VehicleId=m.VehicleId 
                where m.starttime<=d.starttime
                                and m.endtime>=d.endtime

                )t 
        GROUP  BY vehicleid, sno) t1 

Online Demo

输出

+-----------+--------------+-------------+
| vehicleid | ElapsedTime  | NoOfRecords |
+-----------+--------------+-------------+
| 1001      | 02:02:47:000 | 4           |
+-----------+--------------+-------------+
| 1002      | 01:24:10:000 | 2           |
+-----------+--------------+-------------+
| 1003      | 01:14:18:000 | 2           |
+-----------+--------------+-------------+
| 1003      | 00:32:08:000 | 1           |
+-----------+--------------+-------------+