SQL 用于计算两个不同条件的平均值

SQL for calculating average from two different condition

需要计算呼叫中心代表收到线索后的响应时间以及他采取后续行动需要多长时间

计算代表看到 'NEW' 线索并在其上 'Follow_up' 所花费的时间

TABLE T

ID , T_NAME , CRTE_TIMESTMP

123,新,2019-01-01-00.15.32.2820000

456,新,2019-01-01-00.16.23.4320000

123,跟进,2019-01-02-15.31.48.1640000

**结果应该是**

ID , TIME_TOOK

123 , (2019-01-01-00.15.32.2820000) -(2019-01-02-15.31.48.1640000 )

可能看起来像这样

with temp as (
select ID
     , T_NAME
     , CRTE_TIMESTMP,
       lag(CRTE_TIMESTMP) over (partition by id order by CRTE_TIMESTMP) as CRTE_TIMESTMP_lag
from t
) 
select ID
     , hours_between (CRTE_TIMESTMP,CRTE_TIMESTMP_lag ) as time_took
  from temp
 where t_name = 'FOLLOW-UP'

根据您需要的粒度,您还可以使用 seconds_between 或 days_between 或 Db2 11 中提供的任何其他函数