根据多个条件汇总DB2结果

时间:2019-10-04 14:18:29

标签: sql db2 db2-400

我正在尝试根据几种因素找出汇总DB2结果和分组的最佳方法。

当前我有以下查询:

SELECT
    T1.VEHICLE,
    T2.VEHICLE_ID,
    T3.WORK_ORDER_ID,
    T3.JOB_CREATION,
    T5.JOB_STATUS,
    T4.JOB_STATUS_TIME
FROM SCHEMA.VEHICLE T1
INNER JOIN SCHEMA.VEHICLE_TO_WORK_ORDER T2
ON T1.VEHICLE_ID = T2.VEHICLE_ID
INNER JOIN SCHEMA.WORK_ORDER T3
ON T2.WORK_ORDER_ID = T3.WORK_ORDER_ID
INNER JOIN SCHEMA.WORK_ORDER_TO_JOB_STATUS T4
ON T3.WORK_ORDER_ID = T4.WORK_ORDER_ID
INNER JOIN SCHEMA.JOB_STATUS T5
ON T4.JOB_STATUS_ID = T5.JOB_STATUS_ID;

它返回这些结果,从数据角度来看是正确的:

VEHICLE    VEHICLE_ID   WORK_ORDER_ID           JOB_CREATION           JOB_STATUS          JOB_STATUS_TIME          
------------------------------------------------------------------------------------------------------------------
VEHICLE 6     6             12345       2019-09-25 00:00:09.426178      CREATED         2019-09-25 00:00:09.469059
VEHICLE 6     6             12345       2019-09-25 00:00:09.426178      ACTIVE          2019-09-25 13:40:00.981891
VEHICLE 6     6             12345       2019-09-25 00:00:09.426178      COMPLETED       2019-09-25 13:45:02.748800
VEHICLE 7     7             54321       2019-09-26 00:00:09.426178      CREATED         2019-09-26 00:00:09.469059
VEHICLE 7     7             54321       2019-09-26 00:00:09.426178      ACTIVE          2019-09-26 13:40:00.981891
VEHICLE 7     7             54321       2019-09-26 00:00:09.426178      PAUSED          2019-09-26 14:40:02.748800
VEHICLE 7     7             54321       2019-09-26 00:00:09.426178      ACTIVE          2019-09-26 14:45:09.469059
VEHICLE 7     7             54321       2019-09-26 00:00:09.426178      COMPLETED       2019-09-26 14:50:00.981891
VEHICLE 3     3             12346       2019-09-27 00:00:09.426178      OPEN            2019-09-27 13:40:02.748800
VEHICLE 3     3             12346       2019-09-27 00:00:09.426178      ACTIVE          2019-09-27 13:45:09.469059
VEHICLE 3     3             12346       2019-09-27 00:00:09.426178      PAUSED          2019-09-27 13:50:00.981891
VEHICLE 3     3             12346       2019-09-27 00:00:09.426178      CANCELLED       2019-09-27 13:51:02.748800

我要在此处进行的操作是按车辆分组,并在给定的日期范围内获取该车辆的工作指令,然后对活动时间或活动之间的时间进行汇总,以便我可以对汇总列进行汇总(此示例有3辆车,每辆车只有一个工作单,但是我希望能够查看日期范围内的任何工作单并获得相同的汇总。)

我想获得所创建的每个工作订单的计数,以及针对自己的列而以已完成或被取消的每个订单的计数,但是我想要的总活动时间为job_status_time(从每个活动到暂停的时间或处于活动状态或已完成状态,因为任务可以先处于活动状态,然后暂停,然后再次处于活动状态,然后再完成)

我希望获得与此类似的结果,但我只是不太了解如何正确汇总此结果:

VEHICLE    Created    Completed    Cancelled    Total Active Time (minutes)
------------------------------------------------------------------
6           1           1           0               5
7           1           1           0               65
3           1           0           1               5

如何按车辆对这些结果进行分组,并仍然基于job_status获取这些总和列和合计时间

1 个答案:

答案 0 :(得分:1)

LUW的Db2

WITH 
  RES (VEHICLE_ID, JOB_STATUS, JOB_STATUS_TIME) AS 
(
VALUES
  (6, 'CREATED',   TIMESTAMP('2019-09-25-00.00.09.469059'))
, (6, 'ACTIVE',    TIMESTAMP('2019-09-25-13.40.00.981891'))
, (6, 'COMPLETED', TIMESTAMP('2019-09-25-13.45.02.748800'))

, (7, 'CREATED',   TIMESTAMP('2019-09-26-00.00.09.469059'))
, (7, 'ACTIVE',    TIMESTAMP('2019-09-26-13.40.00.981891'))
, (7, 'PAUSED',    TIMESTAMP('2019-09-26-14.40.02.748800'))
, (7, 'ACTIVE',    TIMESTAMP('2019-09-26-14.45.09.469059'))
, (7, 'COMPLETED', TIMESTAMP('2019-09-26-14.50.00.981891'))

, (3, 'OPEN',      TIMESTAMP('2019-09-27-13.40.02.748800'))
, (3, 'ACTIVE',    TIMESTAMP('2019-09-27-13.45.09.469059'))
, (3, 'PAUSED',    TIMESTAMP('2019-09-27-13.50.00.981891'))
, (3, 'CANCELLED', TIMESTAMP('2019-09-27-13.51.02.748800'))
)
, A AS 
(
SELECT 
  VEHICLE_ID, JOB_STATUS
, JOB_STATUS_TIME
, LEAD (JOB_STATUS_TIME) OVER (PARTITION BY VEHICLE_ID ORDER BY JOB_STATUS_TIME) AS JOB_STATUS_TIME_NEXT
FROM RES
)
SELECT
  VEHICLE_ID
, COUNT(CASE JOB_STATUS WHEN 'CREATED'   THEN 1 END) AS CREATED
, COUNT(CASE JOB_STATUS WHEN 'COMPLETED' THEN 1 END) AS COMPLETED
, COUNT(CASE JOB_STATUS WHEN 'CANCELLED' THEN 1 END) AS CANCELLED
, SUM 
  (
  CASE JOB_STATUS WHEN 'ACTIVE' THEN 
    (DAYS(JOB_STATUS_TIME_NEXT) - DAYS(JOB_STATUS_TIME)) * 86400 
  + MIDNIGHT_SECONDS(JOB_STATUS_TIME_NEXT) - MIDNIGHT_SECONDS(JOB_STATUS_TIME) 
  END
  ) / 60 AS ACTIVE_MINUTES
FROM A
GROUP BY VEHICLE_ID;

iSeries和LUW的DB2

似乎DB2 for iSeries(至少是我的7.3)有一个错误-尝试在上面的查询中使用DAYS(JOB_STATUS_TIME_NEXT)表达式会导致SQLCODE = -171。我不知道这是什么原因:是因为是从OLAP函数获得的函数参数,还是因为其他原因...

但是,我们可以如下重写查询:

WITH 
  RES (VEHICLE_ID, JOB_STATUS, JOB_STATUS_TIME) AS 
(
VALUES
  (6, 'CREATED',   TIMESTAMP('2019-09-25-00.00.09.469059'))
, (6, 'ACTIVE',    TIMESTAMP('2019-09-25-13.40.00.981891'))
, (6, 'COMPLETED', TIMESTAMP('2019-09-25-13.45.02.748800'))

, (7, 'CREATED',   TIMESTAMP('2019-09-26-00.00.09.469059'))
, (7, 'ACTIVE',    TIMESTAMP('2019-09-26-13.40.00.981891'))
, (7, 'PAUSED',    TIMESTAMP('2019-09-26-14.40.02.748800'))
, (7, 'ACTIVE',    TIMESTAMP('2019-09-26-14.45.09.469059'))
, (7, 'COMPLETED', TIMESTAMP('2019-09-26-14.50.00.981891'))

, (3, 'OPEN',      TIMESTAMP('2019-09-27-13.40.02.748800'))
, (3, 'ACTIVE',    TIMESTAMP('2019-09-27-13.45.09.469059'))
, (3, 'PAUSED',    TIMESTAMP('2019-09-27-13.50.00.981891'))
, (3, 'CANCELLED', TIMESTAMP('2019-09-27-13.51.02.748800'))
)
, A AS 
(
SELECT 
  VEHICLE_ID, JOB_STATUS
, JOB_STATUS_TIME
, ROWNUMBER() OVER (PARTITION BY VEHICLE_ID ORDER BY JOB_STATUS_TIME) AS RN
FROM RES
)
SELECT
  A1.VEHICLE_ID
, COUNT(CASE A1.JOB_STATUS WHEN 'CREATED'   THEN 1 END) AS CREATED
, COUNT(CASE A1.JOB_STATUS WHEN 'COMPLETED' THEN 1 END) AS COMPLETED
, COUNT(CASE A1.JOB_STATUS WHEN 'CANCELLED' THEN 1 END) AS CANCELLED
, SUM 
  (
  CASE A1.JOB_STATUS WHEN 'ACTIVE' THEN 
    (DAYS(A2.JOB_STATUS_TIME) - DAYS(A1.JOB_STATUS_TIME)) * 86400 
  + MIDNIGHT_SECONDS(A2.JOB_STATUS_TIME) - MIDNIGHT_SECONDS(A1.JOB_STATUS_TIME) 
  END
  ) / 60 AS ACTIVE_MINUTES
FROM A A1
LEFT JOIN A A2 ON A2.VEHICLE_ID = A1.VEHICLE_ID AND A2.RN = A1.RN + 1
GROUP BY A1.VEHICLE_ID;

结果是:

|VEHICLE_ID |CREATED    |COMPLETED  |CANCELLED  |ACTIVE_MINUTES|
|-----------|-----------|-----------|-----------|--------------|
|3          |0          |0          |1          |4             |
|6          |1          |1          |0          |5             |
|7          |1          |1          |0          |64            |