计算Teradata的时间差(Calculate time difference in Terada

2019-10-29 08:38发布

我有一种情况来计算分时间戳的差异。 有2桌一个共同的领域,即dw_job_id并想加入2个表和猜测,计算

(scratch.COGIPF_RUNREPORT_test2.end_ts - concat(proct_dt, scratch.dw_job_sla_dim_test.sla_time)

例如,在减法分钟:

(2018-03-05 01:53:14.201000 - 2018-03-05 08:00:00.000000)= -366

它将使用下面的记录是相当清楚的:

第一次查询有end_ts:

sel * from scratch.COGIPF_RUNREPORT_test2 where  dw_job_id=1226

结果:

dw_job_id   proct_dt              start_ts              end_ts                 time_diff    dw_job_status_id    
1,226       2018-03-05 00:00:00   2018-03-05 01:50:23   2018-03-05 01:53:14.201000  3            12                                                                    
1,226       2018-03-06 00:00:00   2018-03-06 01:42:56   2018-03-06 01:45:23.553000  3            12 

第二个查询:

select * from scratch.dw_job_sla_dim_test  where dw_job_id=1226

结果:

dw_job_id   sla_hour    sla_minute   sla_time
1,226       8             0           08:00:00.000000   

最终的结果应该是:

 dw_job_id  run_date       start_timestamp               end_timestamp           runtime_minutes      sla_miss_minutes

  1,226     3/5/2018    3/5/2018 01:50:23.000000    3/5/2018 01:53:14.201000       2                     -366   
  1,226     3/6/2018    3/6/2018 01:42:56.000000    3/6/2018 01:45:23.553000       2                     -374

例:

减法分钟:-( 2018年3月5日01:53:14.201000 - 2018年3月5日08:00:00.000000)= -366

数据类型:

  sla_hour INTEGER,
  sla_minute INTEGER,
  sla_time TIME(6),
  end_ts VARCHAR(50) CHARACTER SET LATIN NOT CASESPECIFIC,

Answer 1:

我已经在过去使用以下逻辑。 但我认为你是它的追求的。

,(CAST((CAST(end_timestamp AS DATE)- CAST(start_timestamp AS DATE)) AS DECIMAL(18,6)) * 60*24)
  + ((EXTRACT(  HOUR FROM end_timestamp) - EXTRACT(  HOUR FROM start_timestamp))* 60)
  + ((EXTRACT(MINUTE FROM end_timestamp) - EXTRACT(MINUTE FROM start_timestamp))  )
  + ((EXTRACT(SECOND FROM end_timestamp) - EXTRACT(SECOND FROM start_timestamp))/60)
AS "Difference in Minutes"

对于其他人,我会包含我的时间和秒计算,以及

,(CAST((CAST(end_timestamp AS DATE)- CAST(start_timestamp AS DATE)) AS DECIMAL(18,6)) * 60*60*24)
  + ((EXTRACT(  HOUR FROM end_timestamp) - EXTRACT(  HOUR FROM start_timestamp))* 60*60)
  + ((EXTRACT(MINUTE FROM end_timestamp) - EXTRACT(MINUTE FROM start_timestamp)) * 60)
  + ((EXTRACT(SECOND FROM end_timestamp) - EXTRACT(SECOND FROM start_timestamp)))
AS "Difference in Seconds"  


,(CAST((CAST(end_timestamp AS DATE)- CAST(start_timestamp AS DATE)) AS DECIMAL(18,6)) * 24)
  + ((EXTRACT(  HOUR FROM end_timestamp) - EXTRACT(  HOUR FROM start_timestamp)))
  + ((EXTRACT(MINUTE FROM end_timestamp) - EXTRACT(MINUTE FROM start_timestamp)) / 60.000000)
  + ((EXTRACT(SECOND FROM end_timestamp) - EXTRACT(SECOND FROM start_timestamp)) / 3600.000000)
AS "Difference In Hours"    


文章来源: Calculate time difference in Teradata