select DISTINCT EQPGROUP,EQPID,LOCATION,target from (with temp as(SELECT TO_CHAR(TO_DATE(HISTDATE,'YYYYMMDD'),'YYYYMM') MONTH,
'W'||TO_CHAR(TO_DATE(HISTDATE,'YYYYMMDD')+5,'YYWW') WEEK,
HISTDATE,EQPID,EQPTYPE,LOCATION,
case when (RUN_TIME+BKUP_TIME+IDLE_TIME+LOST_TIME+TEST_TIME+TEST_CW_TIME+SUSPD_TIME+WAIT_MFG_TIME+WAIT_ENG_TIME+HOLD_ENG_TIME+
DOWN_TIME+MON_DOWN_TIME+FAC_TIME+PM_TIME+MON_PM_TIME+MON_R_TIME+CIM_TIME+PROCESS_PM_TIME+OFF_TIME)=0 then 0
else (RUN_TIME+BKUP_TIME+IDLE_TIME+LOST_TIME+TEST_TIME+TEST_CW_TIME+SUSPD_TIME)/
(RUN_TIME+BKUP_TIME+IDLE_TIME+LOST_TIME+TEST_TIME+TEST_CW_TIME+SUSPD_TIME+WAIT_MFG_TIME+WAIT_ENG_TIME+HOLD_ENG_TIME+
DOWN_TIME+MON_DOWN_TIME+FAC_TIME+PM_TIME+MON_PM_TIME+MON_R_TIME+CIM_TIME+PROCESS_PM_TIME+OFF_TIME)
END AS UPTIMES
FROM SDB_TB_EQP_HIST
where histdate >= to_char(sysdate-90,'YYYYMMDD')
and (eqpid like 'A%' OR EQPID LIKE 'B%')
ORDER BY TO_CHAR(TO_DATE(HISTDATE,'YYYYMMDD'),'YYYYMM'),TO_CHAR(TO_DATE(HISTDATE,'YYYYMMDD'),'YYWW')
,HISTDATE,EQPID,EQPTYPE,LOCATION)
SELECT A.type,A.histdate,B.EQPGROUP,A.eqpid,B.TARGET,A.EQPTYPE, A.LOCATION,A.UPTIME FROM (
select 'DAY' type,histdate,eqpid,EQPTYPE, LOCATION, ROUND(avg(UPTIMES),4) UPTIME from temp WHERE HISTDATE >= TO_CHAR(TRUNC(SYSDATE-7-0.3125),'YYYYMMDD')
group by histdate,eqpid,EQPTYPE, LOCATION) a
left outer join
(SELECT EQPGROUP, EQPID, TARGET FROM SXRPTUSER.M1_TB_EQPMONITOR_MAINTAIN) B
ON A.EQPID = B.EQPID
order by location,EQPGROUP,eqpid) WHERE 1=1
${if(len(LOCATION) == 0,"","and LOCATION = '" +LOCATION + "'")}
${if(len(EQPID) == 0,"","and EQPID = '" +EQPID + "'")}
${if(len(EQPGROUP) == 0,"","and EQPGROUP = '" +EQPGROUP + "'")}
order by EQPGROUP,EQPID,LOCATION