计算不同时间范围内的项目的平均分钟数

时间:2014-10-29 18:25:28

标签: sql oracle aggregate-functions

example我有一个需要分成两个子集的数据集。每个子集必须仅包括特定时间范围内的项目。然后,我需要采用MAX(最后编辑时间)-MIN(项目添加时间)并除以子集中的项目数量。因此,我的目标是计算在两个时间范围内处理所有项目所需的平均时间。看图。!

我试过这个,但似乎没有用 - 结果不正确;查询在WHERE子句中概述的整个时间段内生成MIN和MAX的结果(因此多天的时间会大大搞砸结果)。

SELECT 
  CASE
    WHEN TO_CHAR(ADDED, 'HH24') BETWEEN 09 AND 11
    THEN TRUNC(((MAX(MODIFIED) - MIN(ADDED))*24*60)/COUNT(TRANSACTIONS))
    ELSE 0
  END 
    +
  CASE
    WHEN TO_CHAR(ADDED, 'HH24') BETWEEN 15 AND 19
    THEN TRUNC(((MAX(MODIFIED) - MIN(ADDED))*24*60)/COUNT(TRANSACTIONS))
    ELSE 0
  END AS subsets_average
FROM TABLE

1 个答案:

答案 0 :(得分:1)

我明白你要做什么。 case语句位于错误的位置。您希望条件 聚合函数:

SELECT TRUNC(((MAX(MODIFIED END)-
               MIN(ADDED ))*24*60)/COUNT(TRANSACTIONS) as grand_average,
       TRUNC(((MAX(TO_CHAR(ADDED, 'HH24') BETWEEN 09 AND 11 THEN MODIFIED END)-
               MIN(TO_CHAR(ADDED, 'HH24') BETWEEN 09 AND 11 THEN ADDED END))*24*60)/COUNT(TRANSACTIONS)
             ) +
       TRUNC(((MAX(TO_CHAR(ADDED, 'HH24') BETWEEN 15 AND 19 THEN MODIFIED END)-
               MIN(TO_CHAR(ADDED, 'HH24') BETWEEN 15 AND 19 THEN ADDED END))*24*60)/COUNT(TRANSACTIONS) AS subsets_average
FROM TABLE