oracle pl / sql日期差的平均值

时间:2018-06-19 18:23:49

标签: sql oracle plsql

我需要从该查询的日期中获取平均值

select service_id,t.arrival_date - 
       lag(t.arrival_date) over 
          (partition by t.service_id order by t.arrival_date) as arrival_date_diff
from table t

我得到的数据就是结果

+000000000 00:37:00.000000
+000000000 00:23:30.000000
+000000000 00:07:04.000000

我尝试使用

AVG(arrival_date_diff) to_date(round(avg(to_number(to_char(arrival_date_diff, 'J')))),'J')

如何从该日期差值中获取平均值并将平均日期转换为分钟?

样本数据:

04/06/18 08:57:34,000000
04/06/18 09:34:34,000000
04/06/18 09:34:34,000000

预期结果:以分钟为单位的平均值(例如:8.5)

预先感谢

1 个答案:

答案 0 :(得分:1)

当您减去两个DATE数据类型值时,结果为天数。例如(基于Scott的模式):

SQL> alter session set nls_date_format = 'dd.mm.yyyy';

Session altered.

SQL> select deptno,
  2    hiredate,
  3    lag(hiredate) over (partition by deptno order by hiredate) lag_hiredate,
  4    --
  5    hiredate - lag(hiredate) over
  6               (partition by deptno order by hiredate) diff
  7  from emp
  8  order by deptno, hiredate;

    DEPTNO HIREDATE   LAG_HIREDA       DIFF
---------- ---------- ---------- ----------
        10 09.06.1981
        10 17.11.1981 09.06.1981        161
        10 23.01.1982 17.11.1981         67
        20 17.12.1980
        20 02.04.1981 17.12.1980        106
        20 03.12.1981 02.04.1981        245
        30 20.02.1981
        30 22.02.1981 20.02.1981          2
        30 01.05.1981 22.02.1981         68
        30 08.09.1981 01.05.1981        130
        30 28.09.1981 08.09.1981         20
        30 03.12.1981 28.09.1981         66

12 rows selected.

SQL>

如果要选择平均差,则必须使用内联视图或CTE,因为AVG不能同时使用分析功能,即avg(lag(...))

最后,根据需要的分钟数,将结果(天,对吗?)乘以24(一天中有24小时)和60(一天中有60分钟):

SQL> with inter as
  2    (select deptno,
  3            hiredate - lag(hiredate) over
  4                       (partition by deptno order by hiredate) diff
  5     from emp
  6  )
  7  select deptno,
  8    avg(diff) avg_diff_days,
  9    --
 10    avg(diff) * (24 * 60) minutes
 11  from inter
 12  group by deptno;

    DEPTNO AVG_DIFF_DAYS    MINUTES
---------- ------------- ----------
        10           114     164160
        20         175,5     252720
        30          57,2      82368

SQL>

[编辑:添加了时间戳示例]

SQL> create table test (datum timestamp);

Table created.

SQL> select * From test;

DATUM
---------------------------------------------------------------------------
04.06.18 08:57:34,000000
04.06.18 09:34:34,000000
04.06.18 09:34:34,000000

SQL>
SQL> select datum - lag(datum) over (order by datum) diff
  2  from test;

DIFF
---------------------------------------------------------------------------

+000000000 00:37:00.000000
+000000000 00:00:00.000000

SQL> -- cast timestamps to dates first, then subtract them; for the final result,
SQL> -- multiply number of days by 24 hours (in a day) and 60 minutes (in an hour)
SQL> select avg(diff) * 24 * 60 avg_minutes
  2  from (select cast(datum as date) - cast(lag(datum) over (order by datum) as date) diff
  3        from test
  4       );

AVG_MINUTES
-----------
       18,5

SQL>
相关问题