1

我在 Teradata 工作,并试图计算工作完成的平均时间。

数据值:

Job Name            Start Date  End Date    End Time
D_BDW_CCIP_SRM_LD   10/10/2012  10/11/2012  01:41:49
D_BDW_CCIP_SRM_LD   10/9/2012   10/10/2012  00:19:56
D_BDW_CCIP_SRM_LD   10/8/2012   10/8/2012   23:37:18
D_BDW_CCIP_SRM_LD   10/5/2012   10/5/2012   23:39:47
D_BDW_CCIP_SRM_LD   10/4/2012   10/4/2012   23:42:47
D_BDW_CCIP_SRM_LD   10/3/2012   10/3/2012   23:41:54

平均值是 16:07 而不是 00:07。我需要发生的是,第二天工作完成的计算理解时间延长了。

在 Excel 中,我可以通过将一天添加到结束时间然后平均并显示为时间来做到这一点。

如何在 Teradata 中执行此操作?

4

3 回答 3

1

这是一个非常有趣的问题! 使用正确的语法更新: 假设您的 START_DATE 和 END_DATE 是DATE值并且 END_TIME 是一个TIME值,这是一个解决方案:

select cast( avg( case
                  when start_date <> end_date
                  then extract(second from end_time)
                     + extract(minute from end_time) * 60 
                     + extract(hour   from end_time) * 3600
                     + 86400
                  else extract(second from end_time) 
                     + extract(minute from end_time) * 60 
                     + extract(hour   from end_time) * 3600
                  end) mod 86400) as decimal(10,4))
      * INTERVAL '00:00:01.00' HOUR TO SECOND as avg_time
from   your_table

正如您在使用 Excel 将自午夜以来的平均秒数确定为中间结果并转换为列时所建议的那样,CASE表达式“添加”一天(86,400 秒) 。TIME

公平地说,我从Teradata 论坛获得了格式化结果的帮助,但我非常喜欢这个,我将自己使用它。

于 2012-10-12T19:13:25.673 回答
0

这似乎可以解决问题,但我很想看看是否还有其他方法。

SELECT job_name,
    case when avg_end_time_in_minutes  > 60*24 then avg_end_time_in_minutes - 60*24 
         else avg_end_time_in_minutes end as avg_adjusted,
case when max_end_time_in_minutes  > 60*24 then max_end_time_in_minutes - 60*24 
     else max_end_time_in_minutes end as max_adjusted,
CAST((CAST(avg_adjusted / 60  AS INTEGER) (FORMAT '9(2)')) AS CHAR(2))||':'||
CAST((CAST((avg_adjusted / 60 MOD 1)*60  AS INTEGER) (FORMAT '9(2)')) AS CHAR(2)) 
                        avg_adjusted_time,
CAST((CAST(max_adjusted / 60  AS INTEGER) (FORMAT '9(2)')) AS CHAR(2))||':'||
CAST((CAST((max_adjusted / 60 MOD 1)*60  AS INTEGER) (FORMAT '9(2)')) AS CHAR(2)) 
                           max_adjusted_time
FROM (
SELECT job_name,
    AVG(end_time_in_minutes) avg_end_time_in_minutes,
    MAX(CAST(end_time_in_minutes AS DECIMAL(8,2))) max_end_time_in_minutes
FROM (
SELECT job_name,
    CAST(substr(end_time, 1, 2) AS INTEGER)*60
        + CAST(substr(end_time, 4, 2) AS INTEGER) 
        + cast(end_date - start_date as integer)*60*24 AS end_time_in_minutes
FROM dabank_prod_ops_tb.bdw_tables_load_tracker_view a          
WHERE a.status = 'COMPLETED' 
AND a.start_date BETWEEN CURRENT_DATE - 31 AND CURRENT_DATE -1
AND a.end_time IS NOT NULL
) a
GROUP BY 1
) b
于 2012-10-11T16:03:20.370 回答
0

首先,计算结束时间从开始日期的午夜开始的秒数。然后我们可以使用它来计算平均花费的秒数,然后将其添加到午夜以找到平均结束时间。

select
    avg(extract(second from end_time) + 60 *
        (extract(minute from end_time) + 60 *
        (extract(hour from end_time) + 24 *
        (end_date - start_date))) as avg_duration_in_seconds
    cast(avg_duration_in_seconds / 60 / 60 as integer) as avg_hours
    mod(cast(avg_duration_in_seconds / 60 as integer), 60) as avg_minutes
    mod(cast(avg_duration_in_seconds as integer), 60) as avg_seconds,
    cast('00:00:00' as time) +
        cast(avg_hours as interval hour) +
        cast(avg_minutes as interval minute) +
        cast(avg_seconds as interval second) as avg_end_time
from my_table

请注意,如果平均值超过 24 小时,avg_end_time 将类似于00:01:15而不是24:01:15.

于 2012-10-12T00:57:48.253 回答