SELECT DATE_TIME,sum(NG_QTY) NC_QTY, sum(OK_QTY) OK_QTY,sum(CHECK_QTY) CHECK_QTY,sum(DISPATCH_QTY) DISPATCH_QTY,FULL_NAME,DUTY_USER,DUTY_DEPART,WORK_ORDER,ITEM_NUMBER,ITEM,ITEM_DESC,OPERATION_DESC,CATEGORY,EMPLOYEE_DESCRIPTION,COMMENTS,DESCRIPTION WORK_CENTER_DESCRIPTION FROM
(
SELECT CASE WHEN ROW_NUMBER() OVER(PARTITION BY zit.sfc ,zit.OPERATION,zit.STEP_ID ORDER BY zit.CREATED_DATE_TIME DESC)=1 AND zit."RESULT"='OK' THEN 0
WHEN ROW_NUMBER() OVER(PARTITION BY zit.sfc ,zit.OPERATION,zit.STEP_ID ORDER BY zit.CREATED_DATE_TIME DESC)=1 AND zit."RESULT"='NG' THEN TO_NUMBER(zab.NC_QTY)
ELSE 0 END NG_QTY, WT.DESCRIPTION,ZNU.FULL_NAME,ZSD.EMPLOYEE,ZSD.EMPLOYEE_DESCRIPTION,CFA.VALUE WORK_ORDER,CFB.VALUE ITEM_NUMBER,O.OPERATION,OT.DESCRIPTION OPERATION_DESC,
ROW_NUMBER() OVER(PARTITION BY zit.sfc,zit.STEP_ID ORDER BY zit.CREATED_DATE_TIME DESC) seq,I.ITEM,IT.DESCRIPTION ITEM_DESC,TO_CHAR(zit.CREATED_DATE_TIME,'YYYY-MM-DD') DATE_TIME,
CASE WHEN ROW_NUMBER() OVER(PARTITION BY zit.sfc ,zit.OPERATION,zit.STEP_ID ORDER BY zit.CREATED_DATE_TIME DESC)=1 AND zit."RESULT"='OK' THEN zsd.DISPATCH_QTY
WHEN ROW_NUMBER() OVER(PARTITION BY zit.sfc ,zit.OPERATION,zit.STEP_ID ORDER BY zit.CREATED_DATE_TIME DESC)=1 AND zit."RESULT"='NG' THEN 0
JOIN OPERATION O ON O.OPERATION = zit.OPERATION AND O.SITE = zit.SITE AND o.CURRENT_REVISION='true'
LEFT JOIN OPERATION_T OT ON OT.OPERATION_BO = O.HANDLE AND OT.LOCALE = 'zh'
JOIN sfc S ON S.SFC=zit.SFC AND s.SITE=zit.SITE
LEFT JOIN SHOP_ORDER SO ON SO.HANDLE = S.SHOP_ORDER_BO
JOIN ITEM I ON I.HANDLE = SO.ITEM_BO
LEFT JOIN ITEM_T IT ON IT.ITEM_BO = S.ITEM_BO
LEFT JOIN CUSTOM_FIELDS CFA ON CFA.HANDLE = SO.HANDLE AND CFA."ATTRIBUTE" = 'WORK_ORDER'
LEFT JOIN CUSTOM_FIELDS CFB ON CFB.HANDLE = SO.HANDLE AND CFB."ATTRIBUTE" = 'ITEM_NUMBER'
JOIN Z_SFC_DISPATCH ZSD ON ZSD.SFC=zit.SFC AND ZSD.SITE=zit.SITE AND zit.STEP_ID=ZSD.STEP_ID
JOIN WORK_CENTER WC ON WC.SITE = zit.SITE AND WC.WORK_CENTER = zit.WORK_CENTER
LEFT JOIN WORK_CENTER_T WT ON WT.WORK_CENTER_BO = WC.HANDLE AND WT.LOCALE = 'zh'
LEFT JOIN Z_ABNORMAL_BILL ZAB ON zit.SFC=ZAB.SFC AND zit.SITE=ZAB.SITE AND zit.STEP_ID=ZAB.STEP_ID AND zab."TYPE"='Z'
LEFT JOIN Z_ABNORMAL_BILL_DISPOSE ZABD ON ZAB.HANDLE=ZABD.ABNORMAL_BILL_BO
LEFT JOIN Z_NWA_USER ZNU ON ZNU.SITE = ZIT.SITE AND ZNU.USER_NAME = ZIT.MODIFY_USER
WHERE zit.SITE= #{site} AND (zit.CATEGORY='P' OR zit.CATEGORY='S') AND zit.STATUS='COMPLETE'
<iftest="workCenter != null and workCenter != ''">
AND zit.WORK_CENTER = #{workCenter}
</if>
<iftest="startFromDate != null and startFromDate != ''">
AND zit.CREATED_DATE_TIME >= TO_DATE(#{startFromDate}, 'YYYY-MM-DD') - 8/24
</if>
<iftest="startToDate != null and startToDate != ''">
AND zit.CREATED_DATE_TIME <= TO_DATE(#{startToDate}|| '15:59:59', 'YYYY-MM-DD HH24:MI:SS')
</if>
<iftest="userId != null and userId != ''">
AND zsd.EMPLOYEE like '%'||#{userId}||'%'
</if>
<iftest="workOrder != null and workOrder != ''">
AND CFA.VALUE LIKE '%'||#{workOrder}||'%'
</if>
GROUP BY ZABD.DUTY_USER,zsd.DISPATCH_QTY,ZABD.DUTY_DEPART,zit."RESULT",zit.sfc ,zit.OPERATION,zit.STEP_ID,CFA.VALUE,CFB.VALUE, zit.CREATED_DATE_TIME,zit.RESRCE,zab.NC_QTY,zit.CREATED_DATE_TIME, ZNU.FULL_NAME,WT.DESCRIPTION,I.ITEM,IT.DESCRIPTION,O.OPERATION,OT.DESCRIPTION,ZSD.EMPLOYEE,ZSD.EMPLOYEE_DESCRIPTION,zit.COMMENTS,zit.CATEGORY
) GROUP BY DATE_TIME,DESCRIPTION,FULL_NAME,DUTY_USER,DUTY_DEPART,WORK_ORDER,ITEM_NUMBER,ITEM,ITEM_DESC,OPERATION,OPERATION_DESC,CATEGORY,EMPLOYEE,EMPLOYEE_DESCRIPTION,COMMENTS
SELECT "SECTION","MONTH",sum(NG_QTY) NC_QTY, sum(OK_QTY) OK_QTY,sum(CHECK_QTY) CHECK_QTY,round(sum(OK_QTY)*100/sum(DISPATCH_QTY),2)||'% ' OK_RATE FROM
(
SELECT CASE WHEN ROW_NUMBER() OVER(PARTITION BY zit.sfc ,zit.OPERATION,zit.STEP_ID ORDER BY zit.CREATED_DATE_TIME DESC)=1 AND zit."RESULT"='OK' THEN 0
WHEN ROW_NUMBER() OVER(PARTITION BY zit.sfc ,zit.OPERATION,zit.STEP_ID ORDER BY zit.CREATED_DATE_TIME DESC)=1 AND zit."RESULT"='NG' THEN TO_NUMBER(zab.NC_QTY)
ELSE 0 END NG_QTY , TO_CHAR(zit.CREATED_DATE_TIME,'YYYY-MM') "MONTH",
CASE WHEN ROW_NUMBER() OVER(PARTITION BY zit.sfc ,zit.OPERATION,zit.STEP_ID ORDER BY zit.CREATED_DATE_TIME DESC)=1 AND zit."RESULT"='OK' THEN zsd.DISPATCH_QTY
WHEN ROW_NUMBER() OVER(PARTITION BY zit.sfc ,zit.OPERATION,zit.STEP_ID ORDER BY zit.CREATED_DATE_TIME DESC)=1 AND zit."RESULT"='NG' THEN 0
ELSE 0 END OK_QTY,CS1.VALUE "SECTION"
,ZSD.DISPATCH_QTY,
ROW_NUMBER() OVER(PARTITION BY zit.sfc ,zit.OPERATION,zit.STEP_ID ORDER BY zit.CREATED_DATE_TIME DESC) seq,
zit."RESULT",COUNT(ZIT.HANDLE) check_qty, zit.sfc ,zit.OPERATION,zit.STEP_ID FROM Z_INSPECTION_TASK zit
JOIN RESRCE R ON zit.SITE = R.SITE AND R.RESRCE = zit.RESRCE
LEFT JOIN CUSTOM_FIELDS CS1 ON CS1.HANDLE = R.HANDLE AND CS1.ATTRIBUTE = 'SECTION'
JOIN sfc S ON S.SFC=zit.SFC AND s.SITE=zit.SITE
JOIN Z_SFC_DISPATCH ZSD ON ZSD.SFC=zit.SFC AND ZSD.SITE=zit.SITE AND zit.STEP_ID=ZSD.STEP_ID
JOIN WORK_CENTER WC ON WC.SITE = zit.SITE AND WC.WORK_CENTER = zit.WORK_CENTER
LEFT JOIN WORK_CENTER_T WT ON WT.WORK_CENTER_BO = WC.HANDLE AND WT.LOCALE = 'zh'
LEFT JOIN Z_ABNORMAL_BILL ZAB ON zit.SFC=ZAB.SFC AND zit.SITE=ZAB.SITE AND zit.STEP_ID=ZAB.STEP_ID AND zab."TYPE"='Z'
WHERE zit.SITE= #{site} AND (zit.CATEGORY='P' OR zit.CATEGORY='S') AND zit.STATUS='COMPLETE'
<iftest="section != null and section != ''">
AND CS1.VALUE = #{section}
</if>
<iftest="startFromDate != null and startFromDate != ''">
AND TO_CHAR(zit.CREATED_DATE_TIME,'YYYY-MM') >=#{startFromDate}
</if>
<iftest="startToDate != null and startToDate != ''">
AND TO_CHAR(zit.CREATED_DATE_TIME,'YYYY-MM') <= #{startToDate}
</if>
GROUP BY zit."RESULT",zit.sfc ,zit.OPERATION,zit.STEP_ID, zit.CREATED_DATE_TIME,zit.RESRCE,zab.NC_QTY,ZSD.DISPATCH_QTY,CS1.VALUE