简体   繁体   English

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

[英]Calculating average minutes for items within different time frames

例 I have a data set that needs to be separated into two subsets. 我有一个数据集,需要将其分为两个子集。 Each subset must include only items within particular time frame. 每个子集只能包含特定时间范围内的项目。 Then, I need to take MAX(last edited time)-MIN(item added time) and divide by amount of items in the subset. 然后,我需要取MAX(上次编辑时间)-MIN(项目添加时间)并除以子集中的项目数量。 Therefore my goal is to calculate AVERAGE amount of time it takes to work on all items within two timeframes. 因此,我的目标是计算两个时间范围内处理所有项目所需的平均时间。 See the picture.! 看到图片了!

I tried this, but it seems not working- results are not correct; 我试过了,但是似乎不起作用-结果不正确。 the query produces result for MIN and MAX for the whole period of time outlined in the WHERE clause (so period of multiple days dramatically screws up the result). 该查询将在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

I see what you are trying to do. 我知道你在做什么。 The case statement is in the wrong place. case陈述书放在错误的位置。 You want conditions inside the aggregation functions: 你想聚合函数的条件:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM