2个时间戳之间的分钟差异

时间:2016-11-18 14:57:38

标签: sql oracle timezone

我正在尝试计算两个时间戳之间的分钟差异,但我得到错误说“无效数字”任何想法为什么会这样?

我试图运行的代码如下:

  TO_CHAR(FROM_TZ(min(q.created_date) over (partition by k.car_id,m.fll_id),     'Europe/London') AT TIME ZONE 'America/New_York', 'DD-MON-YYYY HH24:MI') 
- TO_CHAR(min(m.act_onblk_datt_bu) over (partition by k.car_id, m.fll_id) AT TIME ZONE 'America/New_York', 'DD-MON-YYYY HH24:MI') * 24 * 60) ||'-mins' as difference

当我拆分代码并独立运行时,输出如下:

TO_CHAR(FROM_TZ(min(q.created_date) over (partition by k.car_id,m.fll_id), 'Europe/London') AT TIME ZONE 'America/New_York', 'DD-MON-YYYY HH24:MI') as created_time

30-OCT-2016 21:08:34


TO_CHAR(min(m.act_onblk_datt_bu) over (partition by k.car_id, m.fll_id) AT TIME ZONE 'America/New_York', 'DD-MON-YYYY HH24:MI') as Flight_arrival_Time

30/10/2016 21:06:34

我希望这样做可以减去“创建时间”列和Flight_arrival_time列之间的分钟差异

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:1)

如前所述,您无法从字符串中减去字符串。试试

FROM_TZ(min(q.created_date) over (partition by k.car_id,m.fll_id), 'Europe/London') AT TIME ZONE 'America/New_York'
- min(m.act_onblk_datt_bu) over (partition by k.car_id, m.fll_id) AT TIME ZONE 'America/New_York'

这会返回INTERVAL DAY TO SECOND Data Type

使用CTE中的样本数据进行演示,交叉连接以简化:

alter session set time_zone = 'America/New_York';

with k (car_id) as (select 1 from dual),
  m (fll_id, act_onblk_datt_bu) as (select 1, timestamp '2016-10-30 21:06:34' from dual),
  q (created_date) as (select timestamp '2016-10-31 01:08:34' from dual)
select FROM_TZ(min(q.created_date) over (partition by k.car_id,m.fll_id), 'Europe/London') AT TIME ZONE 'America/New_York'
  - min(m.act_onblk_datt_bu) over (partition by k.car_id, m.fll_id) AT TIME ZONE 'America/New_York'
from m, k, q;

FROM_TZ(MIN(Q.CREAT
-------------------
+00 00:02:00.000000

为了让分钟使用EXTRACT(datetime)

另外注意,为了计算时间戳差异,您不必将它们转换为公共时区。 Oracle在不同时区正确完成。

部分更新

您可以像这样使用EXTRACT

WITH t AS (
    SELECT
        FROM_TZ(min(q.created_date) over (partition by k.car_id,m.fll_id), 'Europe/London') AT TIME ZONE 'America/New_York'
        - min(m.act_onblk_datt_bu) over (partition by k.car_id, m.fll_id) AT TIME ZONE 'America/New_York' AS difference
    FROM q, k, m)
SELECT 60*EXTRACT(HOUR FROM difference) + EXTRACT(MINUTE FROM difference)
FROM t;

CTE即WITH t AS条款只是为了提高可见度。 EXTRACT(MINUTE FROM ...)仅提取间隔的分钟部分,而不是总分钟数。你提到了列" Flight_arrival_time",所以我假设你还要考虑差异时间。也许你必须添加24*60*EXTRACT(DAY FROM difference)

正如我所提到的,您不必将时区转换为时间戳间隔。如果列act_onblk_datt_bu的数据类型为TIMESTAMP WITH TIME ZONETIMESTAMP WITH LOCAL TIME ZONE,您甚至可以将其简化为

WITH t AS (
    SELECT
        FROM_TZ(min(q.created_date) over (partition by k.car_id,m.fll_id), 'Europe/London') 
        - min(m.act_onblk_datt_bu) over (partition by k.car_id, m.fll_id) AS difference
    FROM q, k, m)
SELECT 60*EXTRACT(HOUR FROM difference) + EXTRACT(MINUTE FROM difference)
FROM t;

答案 1 :(得分:0)

Alex Poole已经提供了一种方法来将日期设置为相同的格式。但我试图了解发生了什么,并为您准备了这个样本。

<强> SQL DEMO

此处source是您的查询结果,使用TO_DATE将两个字符串转换为日期,因此格式相同,然后计算差异。

ALTER SESSION SET NLS_LANGUAGE="American"

\\
WITH source as (
    SELECT '30-OCT-2016 21:08:34' as A, '30/10/2016 21:06:34' as B
    FROM Dual
)
SELECT TO_DATE(A, 'DD-MON-YYYY HH24:MI:SS') as new_a,
       TO_DATE(B, 'DD/MM/YYYY HH24:MI:SS') as new_b,
       (  TO_DATE(A, 'DD-MON-YYYY HH24:MI:SS') 
        - TO_DATE(B, 'DD/MM/YYYY HH24:MI:SS')
       ) * 24 * 60 as result
FROM source        

<强>输出

enter image description here