如何获取Oracle中两个时间戳之间的平均值

时间:2017-05-29 07:04:39

标签: sql oracle timestamp extract date-arithmetic

我需要你的帮助。

下面的SQL语句应该用于获取两个时间戳之间的计算毫秒数(e.p_ende和e.p_start之间的差值)。 到目前为止,计算工作正在进行 当我在时间戳上只使用一个过滤器时,它也正常工作。

但我需要两个时间戳之间的值的avg(lz)。如果我使用两个语句,则结果始终为null。

我的第一个陈述如下:

SELECT AVG(LZ)
     FROM ORGANIZATION.V_UI_LOG_SEND A,
         (SELECT E.P_ID, (EXTRACT(DAY FROM E.P_ENDE)*24*60*60*1000
            +EXTRACT(HOUR FROM E.P_ENDE) *60*60*1000
             +EXTRACT(MINUTE FROM E.P_ENDE) *60*1000
             +EXTRACT(SECOND FROM E.P_ENDE) *1000)
            -(EXTRACT(DAY FROM E.P_START)*24*60*60*1000
             +EXTRACT(HOUR FROM E.P_START)*60*60*1000
             +EXTRACT(MINUTE FROM E.P_START)*60*1000
             +EXTRACT(SECOND FROM E.P_START)*1000)AS LZ
         FROM ORGANIZATION.V_UI_LOG_RECEIVE E) E
         WHERE E.P_ID = A.UI_ID
          AND A.P_START >= TO_DATE('20170327070000', 'YYYYMMDDHH24MISS')
          AND A.P_START <= TO_DATE('20170327090000', 'YYYYMMDDHH24MISS');

......第二个是with-clause,就像这样:

`SELECT AVG(ECHTLZ)
     FROM ORGANIZATION.V_UI_LOG_SEND A,
         (SELECT E.P_ID, (EXTRACT(DAY FROM E.P_ENDE)*24*60*60*1000
            +EXTRACT(HOUR FROM E.P_ENDE) *60*60*1000
             +EXTRACT(MINUTE FROM E.P_ENDE) *60*1000
             +EXTRACT(SECOND FROM E.P_ENDE) *1000)
            -(EXTRACT(DAY FROM E.P_START)*24*60*60*1000
             +EXTRACT(HOUR FROM E.P_START)*60*60*1000
             +EXTRACT(MINUTE FROM E.P_START)*60*1000
             +EXTRACT(SECOND FROM E.P_START)*1000)AS ECHTLZ
         FROM ORGANIZATION.V_UI_LOG_RECEIVE E) E
         WHERE E.P_ID = A.UI_ID
          AND A.P_START between TO_DATE('20170327070000', 
             'YYYYMMDDHH24MISS')
          AND TO_DATE('20170327090000', 'YYYYMMDDHH24MISS');`

感谢阅读和帮助! :)

编辑:

非常感谢大家!解决方案要容易得多。我连接到错误的数据库。该表存在于那里,名称相同,但没有数据......第一个声明在整个过程中都正常工作。我很抱歉花了你宝贵的时间!

2 个答案:

答案 0 :(得分:0)

请尝试使用下面提到的代码

SELECT AVG(ECHTLZ)
FROM ORGANIZATION.V_UI_LOG_SEND A,
  (SELECT E.P_ID,
    (E.P_ENDE -E.P_START)*60*24*60 AS ECHTLZ
  FROM ORGANIZATION.V_UI_LOG_RECEIVE E
  ) E
WHERE E.P_ID = A.UI_ID
AND A.P_START BETWEEN TO_DATE('20170327070000', 'YYYYMMDDHH24MISS') AND TO_DATE('20170327090000', 'YYYYMMDDHH24MISS')

这将以秒为单位给出平均值

答案 1 :(得分:0)

如果列是时间戳,则使用TO_TIMESTAMP()而不是to_date()

此查询产生了什么?

SELECT DISTINCT A.UI_ID
FROM ORGANIZATION.V_UI_LOG_SEND A
WHERE A.P_START >= TO_TIMESTAMP('20170327070000', 'YYYYMMDDHH24MISS')
AND A.P_START <= TO_TIMESTAMP('20170327090000', 'YYYYMMDDHH24MISS');

如果没有符合该时间戳范围的行,那么整个查询将为NULL

如果该查询中有行,它们是否与另一个视图中的行匹配?

SELECT DISTINCT A.UI_ID
FROM ORGANIZATION.V_UI_LOG_SEND A
INNER JOIN ORGANIZATION.V_UI_LOG_RECEIVE E ON E.P_ID = A.UI_ID
WHERE A.P_START >= TO_TIMESTAMP('20170327070000', 'YYYYMMDDHH24MISS')
AND A.P_START <= TO_TIMESTAMP('20170327090000', 'YYYYMMDDHH24MISS');

如果该查询中有行,那么您的平均计算也应该有效,虽然您必须满足NULL。(+ EDIT)

SELECT E.P_END, E.P_START
FROM ORGANIZATION.V_UI_LOG_SEND A
INNER JOIN ORGANIZATION.V_UI_LOG_RECEIVE E ON E.P_ID = A.UI_ID
WHERE A.P_START >= TO_TIMESTAMP('20170327070000', 'YYYYMMDDHH24MISS')
AND A.P_START <= TO_TIMESTAMP('20170327090000', 'YYYYMMDDHH24MISS')
AND (E.P_END IS NOT NULL AND E.P_START IS NOT NULL);

如果有这样的数据返回,那么你的平均计算也应该有效。