根据日期和ID列查找第一个值

时间:2019-02-22 11:59:21

标签: sql sql-server azure-sql-data-warehouse

我想找到一个给定的仓库所需的时间值。

下面是创建表和值的代码。我还达到了同一张桌子的其他要求,并且还共享了下面的代码。
我想创建一个新列[StationaryFirstWaitTime],在该列中,可以基于相同的场景获得首次等待时间。
对于给定的ShipmentIdVehicleIdwhere DepotId = StationayId上的[StationaryEndTime] - [StationaryStarttime]获取特定车辆和给定日期在特定日期收到的第一个值shipmentid

下面是代码

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)

我已经编写了用于添加和汇总值并按如下方式计数的代码

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]    

我需要获得如下[StationaryFirstWaitTime]的结果

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

平台:Azure SQL数据仓库

1 个答案:

答案 0 :(得分: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