3

I want to find the values of time taken by a given depot for the stationary.

Below is the code for the create table and values. I have also achieved the other requirements for the same table and also have shared the code below.
I want to create an new column [StationaryFirstWaitTime] where I can get the First wait time for the same scenario based.
For a given ShipmentId, VehicleId, on where DepotId = StationayId get the [StationaryEndTime] - [StationaryStarttime] for the first value which is received on an given date for an specific vehicle and shipmentid.

below is the code

CREATE TABLE [dbo].[Table_Consolidate_Friday](
    [Sno] [int] NOT NULL,
    [VehicleId] [nchar](10) NULL,
    [DepotId] [int] NULL,
    [DepotVisitStartTime] [datetime2](7) NULL,
    [DepotVisitEndTime] [datetime2](7) NULL,
    [StationaryId] [int] NULL,
    [StationaryStartTime] [datetime2](7) NULL,
    [StationaryEndTime] [datetime2](7) NULL,
    [ActualQty] [bigint] NULL,
    [AggreageQty] [bigint] NULL,
    [StationaryWaitTimeTotal] [datetime2](7) NULL,
    [StationaryFirstWaitTime] [datetime2](7) NULL,
    [StationaryRowCount] [bigint] NULL
) ON [PRIMARY]
GO

INSERT [dbo].[Table_Consolidate_Friday] ([Sno], [VehicleId], [DepotId], [DepotVisitStartTime], [DepotVisitEndTime], [StationaryId], [StationaryStartTime], [StationaryEndTime], [ActualQty], [AggreageQty], [StationaryWaitTimeTotal], [StationaryRowCount]) VALUES 
(1, N'TN1       ', 15, '2019-02-15T07:25:33', '2019-02-15T17:25:33', 15, '2019-02-15T07:55:32', '2019-02-15T08:15:23', 10, 119, '2019-02-22T02:02:47', 4),
(1, N'TN1       ', 3,  '2019-02-15T07:25:33', '2019-02-15T17:25:33', 3,  '2019-02-15T09:22:52', '2019-02-15T09:45:59', 20, 119, '2019-02-22T02:02:47', 4),
(1, N'TN1       ', 8,  '2019-02-15T07:25:33', '2019-02-15T17:25:33', 8,  '2019-02-15T11:25:36', '2019-02-15T02:35:37', 33, 119, '2019-02-22T02:02:47', 4),
(1, N'TN1       ', 12, '2019-02-15T07:25:33', '2019-02-15T17:25:33', 12, '2019-02-15T15:15:33', '2019-02-15T15:25:21', 56, 119, '2019-02-22T02:02:47', 4),
(2, N'KA2       ', 23, '2019-02-15T06:12:52', '2019-02-15T11:21:35', 23, '2019-02-15T10:25:13', '2019-02-15T11:15:23', 72, 114, '2019-02-22T01:24:10', 2),
(2, N'KA2       ', 20, '2019-02-15T06:12:52', '2019-02-15T11:21:35', 20, '2019-02-15T07:11:33', '2019-02-15T07:45:33', 42, 114, '2019-02-22T01:24:10', 2),
(3, N'AP3       ', 20, '2019-02-15T06:32:52', '2019-02-15T11:21:35', 20, '2019-02-15T07:13:13', '2019-02-15T08:05:01', 15, 37,  '2019-02-22T01:14:18', 2),
(3, N'AP3       ', 21, '2019-02-15T06:32:52', '2019-02-15T11:21:35', 21, '2019-02-15T09:43:12', '2019-02-15T10:05:42', 22, 37,  '2019-02-22T01:14:18', 2),
(3, N'AP3       ', 15, '2019-02-15T13:12:21', '2019-02-15T19:23:32', 15, '2019-02-15T14:13:13', '2019-02-15T14:45:21', 34, 34,  '2019-02-22T00:32:08', 1)

I have written code to add and aggregate values and count as below

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]    

I need to get the result as below for [StationaryFirstWaitTime] as below

FirstWaitTime
0:-19:-51
0:-19:-51
0:-19:-51
0:-19:-51
0:-50:-10
0:-50:-10
0:-51:-48
0:-51:-48
0:-32:-8

Platform: Azure SQL Datawarehouse

4

1 回答 1

0
  • 窗口聚合函数:FIRST_VALUE
  • 请求的额外列确实具有非标准外观,因此FORMAT()要满足这样的要求:

SQL:

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),

StationaryFirstWaitTime = FORMAT(FIRST_VALUE ( CAST(DATEADD(SECOND,  DATEDIFF(SECOND, StationaryStartTime, StationaryEndTime)  , 0) AS datetime) )       OVER (PARTITION BY Sno, DepotVisitStartTime order by StationaryStartTime), 'H:-m:-s')

FROM [dbo].[Table_Consolidate_Friday]

该额外的兴趣列导致:

StationaryFirstWaitTime
0:-19:-51
0:-19:-51
0:-19:-51
0:-19:-51
0:-34:-0
0:-34:-0
0:-51:-48
0:-51:-48
0:-32:-8

更新:

OP 使用 SQL 数据仓库。FORMAT() 在那里不可用,解决方法:

StationaryFirstWaitTime = REPLACE(CONVERT(VARCHAR(8),FIRST_VALUE ( CAST(DATEADD(SECOND,  DATEDIFF(SECOND, StationaryStartTime, StationaryEndTime)  , 0) AS TIME) )       OVER (PARTITION BY Sno, DepotVisitStartTime order by StationaryStartTime), 8), ':', ':-')

结果:

StationaryFirstWaitTime
00:-19:-51
00:-19:-51
00:-19:-51
00:-19:-51
00:-34:-00
00:-34:-00
00:-51:-48
00:-51:-48
00:-32:-08
于 2019-02-22T12:28:59.417 回答