oracle减去2个日期然后除以计数

时间:2016-02-11 18:48:53

标签: sql oracle average date-difference

我正在尝试(date1 - date2)/ count(date1)

SELECT TO_CHAR(TRUNC(submit_date, 'iw'), 'YYYY-MM-DD') TIMESTAMP
     , ( TO_CHAR( (closed_date - submit_date),'dd/mm/yyyy') )
                                             / count(closed_date)  AVG  
  FROM t_oi_request_form rf
  LEFT JOIN t_oi_requester_domain rd
    ON rf.REQUEST_TYPE = rd.requester_domain_id
  LEFT JOIN t_oi_report_type rt
    ON rf.requestor_domain = rt.REPORT_TYPE_ID
 WHERE submit_date
          between TO_DATE('2015-11-02  00:00:00', 'YYYY-MM-DD hh24:mi:ss')
              AND TO_DATE('2016-02-08 00:00:00', 'YYYY-MM-DD hh24:mi:ss')
 GROUP BY TRUNC(submit_date, 'iw'),
          ( TO_CHAR( (closed_date - submit_date),'dd/mm/yyyy') )
                                                / count(closed_date)
 ORDER BY TRUNC(submit_date, 'iw')

我收到错误Error in query: ORA-00933: SQL command not properly ended

以下是样本数据

SUBMIT_DATE | CLOSED_DATE
---------------------------
  02-NOV-15 |   12-NOV-15
  02-NOV-15 |   03-NOV-15
  04-NOV-15 |   20-JAN-16
  04-NOV-15 |   11-NOV-15
  04-NOV-15 |   19-JAN-16
  05-NOV-15 |   31-DEC-15
  05-NOV-15 |   05-NOV-15
  06-NOV-15 |   06-NOV-15
  05-NOV-15 |   05-NOV-15
  04-NOV-15 |   05-NOV-15
  06-NOV-15 |     NULL
  10-NOV-15 |   11-NOV-15
  13-NOV-15 |   17-NOV-15
  13-NOV-15 |   17-NOV-15
  09-NOV-15 |   11-NOV-15
  11-NOV-15 |   17-NOV-15
  11-NOV-15 |   11-NOV-15
  13-NOV-15 |   17-NOV-15
  11-NOV-15 |   11-NOV-15
  10-NOV-15 |   13-NOV-15
  11-NOV-15 |   11-NOV-15
  19-NOV-15 |   19-NOV-15
  17-NOV-15 |   20-JAN-16
  17-NOV-15 |   18-DEC-15
  19-NOV-15 |   03-DEC-15
  17-NOV-15 |     NULL
  19-NOV-15 |   15-DEC-15
  16-NOV-15 |     NULL
  18-NOV-15 |   19-NOV-15
  20-NOV-15 |   20-NOV-15
  19-NOV-15 |   20-NOV-15
  20-NOV-15 |   25-NOV-15
  20-NOV-15 |   25-NOV-15
  17-NOV-15 |   17-NOV-15
  19-NOV-15 |   20-NOV-15
  19-NOV-15 |   20-NOV-15
  19-NOV-15 |   20-NOV-15
  17-NOV-15 |   17-NOV-15
  17-NOV-15 |   17-NOV-15
  17-NOV-15 |   17-NOV-15
  17-NOV-15 |   17-NOV-15
  17-NOV-15 |   17-NOV-15
  18-NOV-15 |   20-NOV-15
  19-NOV-15 |   20-NOV-15
  19-NOV-15 |   20-NOV-15
  20-NOV-15 |   20-NOV-15
  20-NOV-15 |   20-NOV-15
  26-NOV-15 |   02-DEC-15
  25-NOV-15 |   26-NOV-15
  27-NOV-15 |   01-DEC-15
  25-NOV-15 |   25-NOV-15
  24-NOV-15 |   25-NOV-15
  25-NOV-15 |   01-DEC-15
  26-NOV-15 |   18-JAN-16
  25-NOV-15 |   30-NOV-15
  26-NOV-15 |   06-JAN-16
  26-NOV-15 |   27-NOV-15
  25-NOV-15 |   31-DEC-15
  26-NOV-15 |     NULL
  25-NOV-15 |   31-DEC-15
  25-NOV-15 |   31-DEC-15
  24-NOV-15 |     NULL
  26-NOV-15 |   11-DEC-15
  30-NOV-15 |   01-DEC-15
  03-DEC-15 |   15-DEC-15
  30-NOV-15 |   01-DEC-15
  02-DEC-15 |   04-DEC-15
  03-DEC-15 |   04-DEC-15
  02-DEC-15 |   04-DEC-15
  02-DEC-15 |     NULL
  04-DEC-15 |   04-DEC-15
  03-DEC-15 |   03-DEC-15
  02-DEC-15 |   04-DEC-15
  10-DEC-15 |   16-DEC-15
  09-DEC-15 |   16-DEC-15
  11-DEC-15 |     NULL
  08-DEC-15 |     NULL
  09-DEC-15 |   09-DEC-15
  09-DEC-15 |   09-DEC-15
  09-DEC-15 |     NULL
  10-DEC-15 |     NULL
  15-DEC-15 |   16-DEC-15
  17-DEC-15 |   18-DEC-15
  16-DEC-15 |   29-DEC-15
  15-DEC-15 |   16-DEC-15
  14-DEC-15 |   16-DEC-15
  23-DEC-15 |   06-JAN-16
  24-DEC-15 |   07-JAN-16
  22-DEC-15 |     NULL
  31-DEC-15 |     NULL
  08-JAN-16 |   26-JAN-16
  07-JAN-16 |   11-JAN-16
  07-JAN-16 |   11-JAN-16
  04-JAN-16 |   06-JAN-16
  05-JAN-16 |   06-JAN-16
  04-JAN-16 |   06-JAN-16
  04-JAN-16 |   06-JAN-16
  07-JAN-16 |   11-JAN-16
  06-JAN-16 |   06-JAN-16
  12-JAN-16 |   13-JAN-16
  13-JAN-16 |   20-JAN-16
  11-JAN-16 |   11-JAN-16
  12-JAN-16 |   13-JAN-16
  22-JAN-16 |     NULL
  18-JAN-16 |     NULL
  21-JAN-16 |   22-JAN-16
  19-JAN-16 |   20-JAN-16
  21-JAN-16 |   22-JAN-16
  21-JAN-16 |   22-JAN-16
  19-JAN-16 |     NULL
  22-JAN-16 |     NULL
  19-JAN-16 |     NULL
  20-JAN-16 |   22-JAN-16
  19-JAN-16 |   21-JAN-16
  21-JAN-16 |   22-JAN-16
  21-JAN-16 |   21-JAN-16
  20-JAN-16 |     NULL
  18-JAN-16 |     NULL
  18-JAN-16 |     NULL
  18-JAN-16 |     NULL
  26-JAN-16 |     NULL
  26-JAN-16 |     NULL
  26-JAN-16 |     NULL
  26-JAN-16 |     NULL
  26-JAN-16 |     NULL
  29-JAN-16 |   05-FEB-16
  26-JAN-16 |   05-FEB-16
  29-JAN-16 |     NULL
  29-JAN-16 |     NULL
  26-JAN-16 |   26-JAN-16
  26-JAN-16 |     NULL
  26-JAN-16 |   26-JAN-16
  04-FEB-16 |     NULL
  05-FEB-16 |     NULL
  02-FEB-16 |   05-FEB-16
  04-FEB-16 |     NULL
  01-FEB-16 |     NULL
  02-FEB-16 |   02-FEB-16
  05-FEB-16 |     NULL
  01-FEB-16 |     NULL
  03-FEB-16 |     NULL
  03-FEB-16 |     NULL

我正在尝试计算平均开放时间(天数),我可能做错了

3 个答案:

答案 0 :(得分:1)

这应该为您提供每周submit_dateclosed_date之间的平均时差:

SELECT TRUNC(submit_date, 'iw')
   ,AVG(closed_date - submit_date)
   --,AVG(TRUNC(closed_date) - TRUNC(submit_date)) -- ??

FROM t_oi_request_form rf
LEFT JOIN t_oi_requester_domain rd
  ON rf.REQUEST_TYPE = rd.requester_domain_id
LEFT JOIN t_oi_report_type rt
  ON rf.requestor_domain = rt.REPORT_TYPE_ID
WHERE submit_date between TO_DATE('2015-11-02 00:00:00', 'YYYY-MM-DD hh24:mi:ss')
                      AND TO_DATE('2016-02-08 00:00:00', 'YYYY-MM-DD hh24:mi:ss')
GROUP BY TRUNC(submit_date, 'iw')
ORDER BY TRUNC(submit_date, 'iw')

不需要执行SUM / COUNT,因为聚合函数无论如何都会忽略NULL。如果你不关心NULL,你可以将外连接更改为内部。

答案 1 :(得分:0)

我不是一个神谕,但只是看看你的代码,我可以看到缺少括号:

你有:

SELECT TO_CHAR(TRUNC(submit_date, 'iw'), 'YYYY-MM-DD') TIMESTAMP
,( TO_char( (closed_date - submit_date),'dd/mm/yyyy') ) / count(closed_date)  AVG


FROM t_oi_request_form rf
LEFT JOIN t_oi_requester_domain rd ON rf.REQUEST_TYPE = rd.requester_domain_id
LEFT JOIN t_oi_report_type rt ON rf.requestor_domain = rt.REPORT_TYPE_ID
WHERE submit_date between TO_DATE('2015-11-02  00:00:00', 'YYYY-MM-DD hh24:mi:ss')
    AND TO_DATE('2016-02-08 00:00:00', 'YYYY-MM-DD hh24:mi:ss')
GROUP BY TRUNC(submit_date, 'iw'),( TO_char( (closed_date - submit_date),'dd/mm/yyyy') ) / count(closed_date) )
ORDER BY TRUNC(submit_date, 'iw')

但是你缺少GROUP BY子句中的括号:

SELECT TO_CHAR(TRUNC(submit_date, 'iw'), 'YYYY-MM-DD') [TIMESTAMP]
,( TO_char( (closed_date - submit_date),'dd/mm/yyyy') ) / count(closed_date) [AVG]


FROM t_oi_request_form rf
LEFT JOIN t_oi_requester_domain rd ON rf.REQUEST_TYPE = rd.requester_domain_id
LEFT JOIN t_oi_report_type rt ON rf.requestor_domain = rt.REPORT_TYPE_ID
WHERE submit_date between TO_DATE('2015-11-02  00:00:00', 'YYYY-MM-DD hh24:mi:ss')
    AND TO_DATE('2016-02-08 00:00:00', 'YYYY-MM-DD hh24:mi:ss')
GROUP BY TRUNC(submit_date, 'iw'),(( TO_char( (closed_date - submit_date),'dd/mm/yyyy') ) / count(closed_date) )
ORDER BY TRUNC(submit_date, 'iw')

这应解决:

  

查询错误:ORA-00933:SQL命令未正确结束

答案 2 :(得分:0)

SELECT列表中的表达式似乎有点过于复杂,要计算两个日期之间的天数,然后将它们合并为一个平均值。

我不明白为什么你不会只使用AVG聚合函数:

  AVG( rf.closed_date - rf.submit_date )

就是这样:

SELECT TO_CHAR(TRUNC(rf.submit_date,'iw'),'YYYY-MM-DD') AS TIMESTAMP
     , AVG( rf.closed_date - rf.submit_date )           AS avg_days_to_close
  FROM t_oi_request_form rf
  LEFT
  JOIN t_oi_requester_domain rd
    ON rf.REQUEST_TYPE = rd.requester_domain_id
  LEFT
  JOIN t_oi_report_type rt
    ON rf.requestor_domain = rt.REPORT_TYPE_ID
 WHERE rf.submit_date >= TO_DATE('2015-11-02','YYYY-MM-DD')
   AND rf.submit_date <  TO_DATE('2016-02-08','YYYY-MM-DD')
 GROUP BY TO_CHAR(TRUNC(rf.submit_date,'iw'),'YYYY-MM-DD')
 ORDER BY TO_CHAR(TRUNC(rf.submit_date,'iw'),'YYYY-MM-DD')

我们可以使用SUM()COUNT()和除法运算获得相同的结果:

 SUM( rf.closed_date - rf.submit_date)  / COUNT( rf.closed_date - rf.submit_date)