В приведенных ниже схеме и данных нам нужно найти совокупные значения AggreageQty, StationaryWaitTimeTotal, StationaryRowCount для каждого транспортного средства для указанных дат, и я хочу найти количество остановок для каждой даты с несколькими записями.
CREATE TABLE [dbo].[Table_Consolidate](
[Sno] [int] NOT NULL,
[VehicleId] [nchar](10) NULL,
[DepotVisitStartTime] [datetime2](7) NULL,
[DepotVisitEndTime] [datetime2](7) NULL,
[StationaryStartTime] [datetime2](7) NULL,
[StationaryEndTime] [datetime2](7) NULL,
[ActualQty] [bigint] NULL,
[AggreageQty] [bigint] NULL,
[StationaryWaitTimeTotal] [datetime2](7) NULL,
[StationaryRowCount] [bigint] NULL
) ON [PRIMARY]
GO
INSERT [dbo].[Table_Consolidate] ([Sno], [VehicleId], [DepotVisitStartTime], [DepotVisitEndTime], [StationaryStartTime], [StationaryEndTime], [ActualQty]) VALUES (1, N'TN1 ', CAST(N'2019-02-15T07:25:33.0000000' AS DateTime2), CAST(N'2019-02-15T17:25:33.0000000' AS DateTime2), CAST(N'2019-02-15T07:55:32.0000000' AS DateTime2), CAST(N'2019-02-15T08:15:23.0000000' AS DateTime2), 10)
INSERT [dbo].[Table_Consolidate] ([Sno], [VehicleId], [DepotVisitStartTime], [DepotVisitEndTime], [StationaryStartTime], [StationaryEndTime], [ActualQty]) VALUES (1, N'TN1 ', CAST(N'2019-02-15T07:25:33.0000000' AS DateTime2), CAST(N'2019-02-15T17:25:33.0000000' AS DateTime2), CAST(N'2019-02-15T09:22:52.0000000' AS DateTime2), CAST(N'2019-02-15T09:45:59.0000000' AS DateTime2), 20)
INSERT [dbo].[Table_Consolidate] ([Sno], [VehicleId], [DepotVisitStartTime], [DepotVisitEndTime], [StationaryStartTime], [StationaryEndTime], [ActualQty]) VALUES (1, N'TN1 ', CAST(N'2019-02-15T07:25:33.0000000' AS DateTime2), CAST(N'2019-02-15T17:25:33.0000000' AS DateTime2), CAST(N'2019-02-15T11:25:36.0000000' AS DateTime2), CAST(N'2019-02-15T02:35:37.0000000' AS DateTime2), 33)
INSERT [dbo].[Table_Consolidate] ([Sno], [VehicleId], [DepotVisitStartTime], [DepotVisitEndTime], [StationaryStartTime], [StationaryEndTime], [ActualQty]) VALUES (1, N'TN1 ', CAST(N'2019-02-15T07:25:33.0000000' AS DateTime2), CAST(N'2019-02-15T17:25:33.0000000' AS DateTime2), CAST(N'2019-02-15T15:15:33.0000000' AS DateTime2), CAST(N'2019-02-15T15:25:21.0000000' AS DateTime2), 56)
INSERT [dbo].[Table_Consolidate] ([Sno], [VehicleId], [DepotVisitStartTime], [DepotVisitEndTime], [StationaryStartTime], [StationaryEndTime], [ActualQty]) VALUES (2, N'KA2 ', CAST(N'2019-02-15T06:12:52.0000000' AS DateTime2), CAST(N'2019-02-15T11:21:35.0000000' AS DateTime2), CAST(N'2019-02-15T10:25:13.0000000' AS DateTime2), CAST(N'2019-02-15T11:15:23.0000000' AS DateTime2), 72)
INSERT [dbo].[Table_Consolidate] ([Sno], [VehicleId], [DepotVisitStartTime], [DepotVisitEndTime], [StationaryStartTime], [StationaryEndTime], [ActualQty]) VALUES (2, N'KA2 ', CAST(N'2019-02-15T06:12:52.0000000' AS DateTime2), CAST(N'2019-02-15T11:21:35.0000000' AS DateTime2), CAST(N'2019-02-15T07:11:33.0000000' AS DateTime2), CAST(N'2019-02-15T07:45:33.0000000' AS DateTime2), 42)
INSERT [dbo].[Table_Consolidate] ([Sno], [VehicleId], [DepotVisitStartTime], [DepotVisitEndTime], [StationaryStartTime], [StationaryEndTime], [ActualQty]) VALUES (3, N'AP3 ', CAST(N'2019-02-15T06:32:52.0000000' AS DateTime2), CAST(N'2019-02-15T11:21:35.0000000' AS DateTime2), CAST(N'2019-02-15T07:13:13.0000000' AS DateTime2), CAST(N'2019-02-15T08:05:01.0000000' AS DateTime2), 15)
INSERT [dbo].[Table_Consolidate] ([Sno], [VehicleId], [DepotVisitStartTime], [DepotVisitEndTime], [StationaryStartTime], [StationaryEndTime], [ActualQty]) VALUES (3, N'AP3 ', CAST(N'2019-02-15T06:32:52.0000000' AS DateTime2), CAST(N'2019-02-15T11:21:35.0000000' AS DateTime2), CAST(N'2019-02-15T09:43:12.0000000' AS DateTime2), CAST(N'2019-02-15T10:05:42.0000000' AS DateTime2), 22)
INSERT [dbo].[Table_Consolidate] ([Sno], [VehicleId], [DepotVisitStartTime], [DepotVisitEndTime], [StationaryStartTime], [StationaryEndTime], [ActualQty]) VALUES (3, N'AP3 ', CAST(N'2019-02-15T13:12:21.0000000' AS DateTime2), CAST(N'2019-02-15T19:23:32.0000000' AS DateTime2), CAST(N'2019-02-15T14:13:13.0000000' AS DateTime2), CAST(N'2019-02-15T14:45:21.0000000' AS DateTime2), 34)
Ожидаемый результат для значений:
AggreageQty StationaryWaitTimeTotal StationaryRowCount
119 02:02:47:000 4
119 02:02:47:000 4
119 02:02:47:000 4
119 02:02:47:000 4
114 01:24:10:000 2
114 01:24:10:000 2
37 01:14:18:000 2
37 01:14:18:000 2
34 00:32:08:000 1
У меня также есть другие столбцы, которые должны быть добавлены в результат Col1, Col2, Col3, которые являются частью [Table_Consolidate]