Oracle从子查询中获取持续时间的平均值

时间:2017-11-23 09:01:10

标签: sql oracle

大家好日子,我是Oracle SQL的新手。我在这里要做的是从我所做的子查询得到平均值。以下是查询: -

SELECT TO_CHAR (TRUNC (SYSDATE) + (GATEIN_TIME_START - GATEIN_TIME_END), 'hh24:mi:ss' )as duration
FROM GATE_PASS LEFT JOIN GATE_LANE ON (GATE_PASS_ID = GATE_LANE_PASS_ID)
WHERE (GATEIN_TIME_START BETWEEN TO_DATE ('20171123000000', 'YYYYMMDDHH24MISS') 
AND TO_DATE ('20171123005959', 'YYYYMMDDHH24MISS')) AND GATE_LANE_NO = 'GATE_1'

以上查询是工作并返回以下数据行: -

Duration
----------
00:00:50
00:00:17
00:00:13
00:00:43
00:00:43
00:00:37
00:00:18
00:00:17
00:00:18
00:00:18

如您所见,我有10行持续时间。但是,我希望得到这些持续时间的平均值。然后,我在下面查询: -

SELECT AVG
     (SELECT TO_CHAR (TRUNC (SYSDATE) + (GATEIN_TIME_START - GATEIN_TIME_END), 'hh24:mi:ss' )as duration
     FROM GATE_PASS LEFT JOIN GATE_LANE ON (GATE_PASS_ID = GATE_LANE_PASS_ID)
     WHERE (GATEIN_TIME_START BETWEEN TO_DATE ('20171123000000', 'YYYYMMDDHH24MISS') 
     AND TO_DATE ('20171123005959', 'YYYYMMDDHH24MISS')) AND GATE_LANE_NO = 'GATE_1')as average
FROM GATE_PASS
GROUP BY average

正如您所看到的,我将先前的查询作为我的子查询进入我的新创建的查询,以获取这些持续时间的平均值。但是,我收到的错误是: -

[Exception, Error code 936, SQLState 42000] ORA-00936: missing expression

我的错误是什么,或者我做错了什么?非常感谢您的帮助。 谢谢。

3 个答案:

答案 0 :(得分:2)

首先,平均功能应该用于数值。

因此,减去2个日期和时间将返回没有天数(分数为分钟),需要转换为分钟并找到平均值。

试试这个..它应该有效

 SELECT  Avg((GATEIN_TIME_START - GATEIN_TIME_END) * 24 * 60 ) as avg_duration
     FROM GATE_PASS 
          LEFT JOIN GATE_LANE ON (GATE_PASS_ID = GATE_LANE_PASS_ID)
     WHERE 
        GATEIN_TIME_START BETWEEN TO_DATE ('20171123000000', 'YYYYMMDDHH24MISS')  AND TO_DATE ('20171123005959', 'YYYYMMDDHH24MISS')
    AND GATE_LANE_NO = 'GATE_1'

我希望这对你有用。

答案 1 :(得分:1)

当您减去两个日期时,您会得到天数的差异。您可以取平均值,然后使用NUMTODSINTERVAL将平均值转换为间隔数据类型。

您也不需要从字符串转换日期,因为您可以使用日期文字(即DATE '2017-11-23')。

SQL Fiddle

Oracle 11g R2架构设置

CREATE TABLE GATE_PASS (
  GATE_PASS_ID      INTEGER PRIMARY KEY,
  GATEIN_TIME_START DATE,
  GATEIN_TIME_END   DATE
);

CREATE TABLE GATE_LANE (
  GATE_LANE_PASS_ID INTEGER REFERENCES GATE_PASS ( GATE_PASS_ID ),
  GATE_LANE_NO      VARCHAR2(6)
);

INSERT INTO GATE_PASS
SELECT 1, DATE '2017-11-23', DATE '2017-11-23' + INTERVAL '00:00:50' HOUR TO SECOND FROM DUAL UNION ALL
SELECT 2, DATE '2017-11-23', DATE '2017-11-23' + INTERVAL '00:00:17' HOUR TO SECOND FROM DUAL UNION ALL
SELECT 3, DATE '2017-11-23', DATE '2017-11-23' + INTERVAL '00:00:13' HOUR TO SECOND FROM DUAL UNION ALL
SELECT 4, DATE '2017-11-23', DATE '2017-11-23' + INTERVAL '00:00:43' HOUR TO SECOND FROM DUAL UNION ALL
SELECT 5, DATE '2017-11-23', DATE '2017-11-23' + INTERVAL '00:00:43' HOUR TO SECOND FROM DUAL UNION ALL
SELECT 6, DATE '2017-11-23', DATE '2017-11-23' + INTERVAL '00:00:37' HOUR TO SECOND FROM DUAL UNION ALL
SELECT 7, DATE '2017-11-23', DATE '2017-11-23' + INTERVAL '00:00:18' HOUR TO SECOND FROM DUAL UNION ALL
SELECT 8, DATE '2017-11-23', DATE '2017-11-23' + INTERVAL '00:00:17' HOUR TO SECOND FROM DUAL UNION ALL
SELECT 9, DATE '2017-11-23', DATE '2017-11-23' + INTERVAL '00:00:18' HOUR TO SECOND FROM DUAL UNION ALL
SELECT 10, DATE '2017-11-23', DATE '2017-11-23' + INTERVAL '00:00:18' HOUR TO SECOND FROM DUAL;

INSERT INTO GATE_LANE
SELECT LEVEL, 'GATE_1' FROM DUAL CONNECT BY LEVEL <= 10;

查询1

SELECT NUMTODSINTERVAL( 
         AVG( GATEIN_TIME_END - GATEIN_TIME_START ),
         'DAY'
       ) AS duration
FROM   GATE_PASS
       LEFT JOIN GATE_LANE
       ON (GATE_PASS_ID = GATE_LANE_PASS_ID)
WHERE  GATEIN_TIME_START >= DATE '2017-11-23'
AND    GATEIN_TIME_START <  DATE '2017-11-23' + INTERVAL '1' DAY
AND    GATE_LANE_NO = 'GATE_1'

<强> Results

|   DURATION |
|------------|
| 0 0:0:27.4 |

答案 2 :(得分:0)

使用“提取”退出不同的部分并将其添加,然后计算平均值。

Select 
avg(extract(second from T.duration)
        + extract(minute from T.duration) * 60
        + extract(hour from T.duration) * 60 * 60
        ) average

from (
    SELECT TO_CHAR (TRUNC (SYSDATE) + (GATEIN_TIME_START - GATEIN_TIME_END), 'hh24:mi:ss' )as duration
    FROM GATE_PASS LEFT JOIN GATE_LANE ON (GATE_PASS_ID = GATE_LANE_PASS_ID)
    WHERE (GATEIN_TIME_START BETWEEN TO_DATE ('20171123000000', 'YYYYMMDDHH24MISS') 
    AND TO_DATE ('20171123005959', 'YYYYMMDDHH24MISS')) AND GATE_LANE_NO = 'GATE_1'
) T