2012-10-11 136 views
1

我正在Teradata工作,我正在努力計算作業完成的平均時間。計算24小時內的平均時間

數據值:

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中執行此操作?

+0

您是否在尋找工作執行的平均持續時間或工作的中位數結束時間? –

回答

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 
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

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 

CASE表達「增加了」一天(86400秒)如你所說,當使用Excel確定從午夜到中間結果的平均秒數,並轉換爲TIME列。

爲了公平起見,我獲得了幫助從Teradata Forum格式化的結果,但我喜歡這個這麼多,我會用它自己。