在下面的架构和数据中,我们需要找到给定日期的每辆车的累积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]的一部分
答案 0 :(得分:0)
您可以:
<configuration>
<property>
<name>fs.defaultFS</name>
<value>hdfs://localhost:9000</value>
</property>
</configuration>
输出:
SELECT
SUM(tc.ActualQty) OVER(PARTITION BY tc.Sno, tc.DepotVisitStartTime) AggreageQty
, CAST(DATEADD(ms, SUM(DATEDIFF(SECOND, StationaryStartTime, StationaryEndTime) )
OVER (PARTITION BY Sno, tc.DepotVisitStartTime) * 1000, 0) AS TIME)
AS StationaryWaitTimeTotal
, COUNT(tc.ActualQty) OVER(PARTITION BY tc.Sno, tc.DepotVisitStartTime) StationaryRowCount
FROM dbo.Table_Consolidate tc
答案 1 :(得分:0)
使用以下代码获取累积汇总以及其他列。
SELECT *
,SUM(ActualQty) OVER(PARTITION BY VehicleId) AS AggregateQty
,(SUM(DATEDIFF(SECOND,StationaryStartTime,StationaryEndTime)) OVER(PARTITION BY VehicleId))/3600 AS StattionaryWaitTimeTotal
,ROW_NUMBER() OVER(PARTITION BY VehicleId ORDER BY Sno) AS StationaryRowCount
FROM [dbo].[Table_Consolidate]
答案 2 :(得分:0)
示例数据中的第三行会产生负的平稳时间:
StationaryStartTime = 2019-02-15 11:25:36.0000000
StationaryEndTime = 2019-02-15 02:35:37.0000000
-- the correct value is:
StationaryEndTime = 2019-02-15 12:35:37.0000000
固定样本数据后,可以通过以下方式检索期望值:
SELECT
AggreageQty = SUM(ActualQty) OVER (PARTITION BY Sno, DepotVisitStartTime),
StationaryWaitTimeTotal = CAST(DATEADD(SECOND, SUM(DATEDIFF(SECOND, StationaryStartTime, StationaryEndTime) ) OVER (PARTITION BY Sno, DepotVisitStartTime), 0) AS TIME),
StationaryRowCount = COUNT(*) OVER (PARTITION BY Sno, DepotVisitStartTime)
FROM [dbo].[Table_Consolidate]
结果至:
AggreageQty StationaryWaitTimeTotal StationaryRowCount
119 02:02:47 4
119 02:02:47 4
119 02:02:47 4
119 02:02:47 4
114 01:24:10 2
114 01:24:10 2
37 01:14:18 2
37 01:14:18 2
34 00:32:08 1