需要每隔最后一列值更新15分钟

时间:2017-07-28 07:44:13

标签: oracle plsql

Query result screenshot

select ROUTE_DESC,
       TRANSIT_TIME,
       to_timestamp(TIME_DEPART) + 
                     numtodsinterval(TRANSIT_TIME,'HOUR') as "arrival",
       (to_timestamp(TIME_DEPART)
                    +numtodsinterval(nvl(TRANSIT_TIME,0),'HOUR')) 
                          + interval '15' MINUTE as "dept"
  from FE_JPROUTE_SHIP_TMP
 where TRIP_NO='TRP0000464' 
   and GUID='-1259520161' order by SEQ_NO;

我想要这样的输出

route_desc  transit_time  arraival                          dept
-----------------------------------------------------------------
null        null          25-JUL-17 06.15.00.000000000 AM
null        1             25-JUL-17 07.15.00.000000000 AM   25-JUL-17 07.30.00.000000000 AM
null        3             25-JUL-17 10.30.00.000000000 AM   25-JUL-17 10.45.00.000000000 AM

Desired result screenshot

1 个答案:

答案 0 :(得分:0)

您需要使用累积SUM() - ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW - 这是SUM () OVER (ORDER BY )的oracle默认值 以及NULLS FIRST条款。

以下是查询的模拟。

WITH FE_JPROUTE_SHIP_TMP AS
  (SELECT NULL ROUTE_DESC,
    NULL transit_time,
    '25-JUL-17 06.00.00.000000000 AM' TIME_DEPART
  FROM DUAL
  UNION ALL
  SELECT NULL ROUTE_DESC,
    1 transit_time,
    '25-JUL-17 06.00.00.000000000 AM' TIME_DEPART
  FROM DUAL
  UNION ALL
  SELECT NULL ROUTE_DESC,
    3 transit_time,
    '25-JUL-17 06.00.00.000000000 AM' TIME_DEPART
  FROM DUAL
  )
SELECT transit_time,
  arrival,
  NVL2( arrival,arrival + interval '15' MINUTE ,to_timestamp(TIME_DEPART ) + interval '15' MINUTE ) AS "dept"
FROM
  (SELECT transit_time,
    time_depart,
    to_timestamp(TIME_DEPART ) + numtodsinterval(CUMU_TRANSIT_TIME,'HOUR') + numtodsinterval(15*CUMU_DEP_INTV,'MINUTE') arrival
  FROM
    (SELECT transit_time,
      time_depart,
      SUM ( TRANSIT_TIME ) OVER ( ORDER BY transit_time NULLS FIRST ) CUMU_TRANSIT_TIME,
      SUM ( NVL2(TRANSIT_TIME,1,NULL )) OVER ( ORDER BY transit_time NULLS FIRST ) CUMU_DEP_INTV
    FROM FE_JPROUTE_SHIP_TMP
    )
  );

您必须将其合并到主查询中。 让我知道您的反馈意见。