Дата и количество в одной таблице для данного идентификатора - PullRequest
0 голосов
/ 21 февраля 2019

В приведенных ниже схеме и данных нам нужно найти совокупные значения 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]

Ответы [ 3 ]

0 голосов
/ 21 февраля 2019

Используйте приведенный ниже код для получения совокупных агрегаций вместе с другими столбцами.

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]
0 голосов
/ 21 февраля 2019

Ваша третья строка в данных образца генерирует отрицательное стационарное время:

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
0 голосов
/ 21 февраля 2019

Вы можете:

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

ВЫХОД:

AggreageQty            StationaryWaitTimeTotal  StationaryRowCount
119                        16:02:47.0000000             4
119                        16:02:47.0000000             4
119                        16:02:47.0000000             4
119                        16:02:47.0000000             4
114                        01:24:10.0000000             2
114                        01:24:10.0000000             2
37                         01:14:18.0000000             2
37                         01:14:18.0000000             2
34                         00:32:08.0000000             1
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...