如何将平均值与总和相加

时间:2018-09-12 02:55:33

标签: sql oracle

我的查询是:

select
TIERING_2, ASP_NAME , 
sum(case when  SUBCASE_MEASURED = '1' AND FE_TYPE = 'ASP' AND CLOSE_MONTH = '3' and region = 'EEMEA' AND SCHEDULED_RESCHEDULED = 'Scheduled' then 1 else 0 end) DA_SCHEDULED,
sum(case when  SUBCASE_MEASURED = '1' AND FE_TYPE = 'ASP' AND CLOSE_MONTH = '3' and region = 'EEMEA' AND SCHEDULED_RESCHEDULED = 'Reactive'  then 1 else 0 end) FE_DELAY,
sum(case when  SUBCASE_MEASURED = '1' AND FE_TYPE = 'ASP' AND CLOSE_MONTH = '3' and region = 'EEMEA' AND SCHEDULED_RESCHEDULED = 'Reactive'  then 1 else 0 end)Onsite_Time,
sum(case when  SUBCASE_MEASURED = '1' AND FE_TYPE = 'ASP' AND  region = 'EEMEA' AND SCHEDULED_RESCHEDULED = 'Reactive' and LATE_START_CONF_INDICATOR = '0'then 1 else 0 end) Late_hours,
sum(case when  SUBCASE_MEASURED = '1' AND FE_TYPE = 'ASP' AND CLOSE_MONTH = '3' AND ASP_ON_TIME = '1' then 1 else 0 end) ON_TIME
from table_Control_kpi GROUP BY ASP_NAME , TIERING_2

但是,我希望总和平均现场时间。 有人可以帮我将下面的查询与上面的查询结合起来

SELECT AVG(ONSITE_TIME) 
FROM table_Control_kpi
WHERE SCHEDULED_RESCHEDULED = 'Reactive' 
  AND ASP_NAME='3D Networks' 
  AND SUBCASE_MEASURED = '1' 
  AND FE_TYPE = 'ASP' 
  AND CLOSE_MONTH = '3'

1 个答案:

答案 0 :(得分:4)

也许使用window function

SELECT TIERING_2, ASP_NAME ,
       --all the sums up there
       ,AVG(CASE 
          WHEN SCHEDULED_RESCHEDULED = 'Reactive' 
          AND ASP_NAME='3D Networks' 
          AND SUBCASE_MEASURED = '1' 
          AND FE_TYPE = 'ASP' 
          AND CLOSE_MONTH = '3'
        THEN ONSITE_TIME END) OVER ()
FROM table_Control_kpi GROUP BY ASP_NAME , TIERING_2