SQL 高级查询

时间:2024-12-15 18:04:38
WITH FINISHMAINT (FRAMENO,DELIVEREDDATE) AS
(
SELECT DISTINCT RP.FRAMENO, MAX(PRC.DELIVEREDDATE) OVER(PARTITION BY RP.FRAMENO) FROM RT_REPAIR RP
LEFT JOIN RT_REPAIRPART P ON RP.REPAIRNO=P.REPAIRNO
LEFT JOIN RT_RepairProcess PRC ON RP.REPAIRNO=PRC.REPAIRNO
WHERE RP.StatusCode = ''AND value(RP.Deleted,'') = ''
AND P.PARTNO LIKE '15400%' AND VALUE(P.ActualOutQty,0)-VALUE(P.ReturnedQty,0)>0
), DELAYTASKS(FRAMENO,NEXTSTEPTIME) AS
(
SELECT DEF.FRAMENO,RECDT.NEXTSTEPTIME FROM RT_CALLTASKRECDT RECDT
LEFT JOIN RT_CALLTASKDEF DEF ON RECDT.TASKID=DEF.TASKID
WHERE RECDT.Conclusion='' AND RECDT.NextStepTime IS NOT NULL AND YEAR(RECDT.NextStepTime) = 2015
AND (DEF.CANCELED = '' OR (DEF.CANCELED = '' AND RECDT.NEXTSTEPTIME > DEF.CANCELTIME)
)
) SELECT COUNT(1) AS DELAYTASKCOUNT FROM DELAYTASKS LEFT JOIN FINISHMAINT ON DELAYTASKS.FRAMENO=FINISHMAINT.FRAMENO
WHERE FINISHMAINT.DELIVEREDDATE > DELAYTASKS.NEXTSTEPTIME ------------------------------------------------------
WITH FINISHMAINT (FRAMENO,DELIVEREDDATE) AS
(
SELECT DISTINCT RP.FRAMENO, MAX(PRC.DELIVEREDDATE) OVER(PARTITION BY RP.FRAMENO) FROM RT_REPAIR RP
LEFT JOIN RT_REPAIRPART P ON RP.REPAIRNO=P.REPAIRNO
LEFT JOIN RT_RepairProcess PRC ON RP.REPAIRNO=PRC.REPAIRNO
WHERE RP.StatusCode = ''AND value(RP.Deleted,'') = ''
AND P.PARTNO LIKE '15400%' AND VALUE(P.ActualOutQty,0)-VALUE(P.ReturnedQty,0)>0
), DELAYTASKS(FRAMENO,NEXTSTEPTIME) AS
(
SELECT DEF.FRAMENO,RECDT.NEXTSTEPTIME FROM RT_CALLTASKRECDT RECDT
LEFT JOIN RT_CALLTASKDEF DEF ON RECDT.TASKID=DEF.TASKID
WHERE RECDT.Conclusion='' AND RECDT.NextStepTime IS NOT NULL AND YEAR(RECDT.NextStepTime) = 2015
AND (DEF.CANCELED = '' OR (DEF.CANCELED = '' AND RECDT.NEXTSTEPTIME > DEF.CANCELTIME)
)
) SELECT MONTH(DELAYTASKS.NEXTSTEPTIME) MON,COUNT(1) AS DELAYTASKCOUNT FROM DELAYTASKS LEFT JOIN FINISHMAINT ON DELAYTASKS.FRAMENO=FINISHMAINT.FRAMENO
WHERE FINISHMAINT.DELIVEREDDATE > DELAYTASKS.NEXTSTEPTIME
GROUP BY MONTH(DELAYTASKS.NEXTSTEPTIME)
-----------------------------------------
WITH FINISHMAINT (FRAMENO,DELIVEREDDATE) AS
(
SELECT DISTINCT RP.FRAMENO, MAX(PRC.DELIVEREDDATE) OVER(PARTITION BY RP.FRAMENO) FROM RT_REPAIR RP
LEFT JOIN RT_REPAIRPART P ON RP.REPAIRNO=P.REPAIRNO
LEFT JOIN RT_RepairProcess PRC ON RP.REPAIRNO=PRC.REPAIRNO
WHERE RP.StatusCode = ''AND value(RP.Deleted,'') = ''
AND P.PARTNO LIKE '15400%' AND VALUE(P.ActualOutQty,0)-VALUE(P.ReturnedQty,0)>0
), DELAYTASKS(FRAMENO,NEXTSTEPTIME) AS
(
SELECT DEF.FRAMENO,RECDT.NEXTSTEPTIME FROM RT_CALLTASKRECDT RECDT
LEFT JOIN RT_CALLTASKDEF DEF ON RECDT.TASKID=DEF.TASKID
WHERE RECDT.Conclusion='' AND RECDT.NextStepTime IS NOT NULL AND YEAR(RECDT.NextStepTime) = 2015
AND (DEF.CANCELED = '' OR (DEF.CANCELED = '' AND RECDT.NEXTSTEPTIME > DEF.CANCELTIME)
)
) SELECT MON,SUM(DELAYTASKCOUNT) AS DELAYTASKCOUNT FROM (
SELECT 1 AS MON,0 AS DELAYTASKCOUNT FROM SYSIBM.SYSDUMMY1
UNION ALL
SELECT 2 AS MON,0 AS DELAYTASKCOUNT FROM SYSIBM.SYSDUMMY1
UNION ALL
SELECT 3 AS MON,0 AS DELAYTASKCOUNT FROM SYSIBM.SYSDUMMY1
UNION ALL
SELECT 4 AS MON,0 AS DELAYTASKCOUNT FROM SYSIBM.SYSDUMMY1
UNION ALL
SELECT 5 AS MON,0 AS DELAYTASKCOUNT FROM SYSIBM.SYSDUMMY1
UNION ALL
SELECT 6 AS MON,0 AS DELAYTASKCOUNT FROM SYSIBM.SYSDUMMY1
UNION ALL
SELECT 7 AS MON,0 AS DELAYTASKCOUNT FROM SYSIBM.SYSDUMMY1
UNION ALL
SELECT 8 AS MON,0 AS DELAYTASKCOUNT FROM SYSIBM.SYSDUMMY1
UNION ALL
SELECT 9 AS MON,0 AS DELAYTASKCOUNT FROM SYSIBM.SYSDUMMY1
UNION ALL
SELECT 10 AS MON,0 AS DELAYTASKCOUNT FROM SYSIBM.SYSDUMMY1
UNION ALL
SELECT 11 AS MON,0 AS DELAYTASKCOUNT FROM SYSIBM.SYSDUMMY1
UNION ALL
SELECT 12 AS MON,0 AS DELAYTASKCOUNT FROM SYSIBM.SYSDUMMY1
UNION ALL
SELECT MONTH(DELAYTASKS.NEXTSTEPTIME) MON,COUNT(1) AS DELAYTASKCOUNT FROM DELAYTASKS LEFT JOIN FINISHMAINT ON DELAYTASKS.FRAMENO=FINISHMAINT.FRAMENO WHERE FINISHMAINT.DELIVEREDDATE > DELAYTASKS.NEXTSTEPTIME
GROUP BY MONTH(DELAYTASKS.NEXTSTEPTIME)
) T GROUP BY MON --------------------------------------------------
WITH FINISHMAINT (FRAMENO,DELIVEREDDATE) AS
(
SELECT DISTINCT RP.FRAMENO, MAX(PRC.DELIVEREDDATE) OVER(PARTITION BY RP.FRAMENO) FROM RT_REPAIR RP
LEFT JOIN RT_REPAIRPART P ON RP.REPAIRNO=P.REPAIRNO
LEFT JOIN RT_REPAIRPROCESS PRC ON RP.REPAIRNO=PRC.REPAIRNO
WHERE RP.STATUSCODE = ''AND VALUE(RP.DELETED,'') = ''
AND P.PARTNO LIKE '15400%' AND VALUE(P.ACTUALOUTQTY,0)-VALUE(P.RETURNEDQTY,0)>0
), TBACK(FRAMENO,EXPECTSTARTDATE) AS
(
SELECT DEF.FRAMENO,DEF.EXPECTSTARTDATE FROM RT_CALLTASKDEF DEF
WHERE DEF.EXPECTSTARTDATE >= '2014-12-01' AND DEF.EXPECTSTARTDATE<='2015-11-30'
) SELECT MONTH(TBACK.EXPECTSTARTDATE + 1 MONTH) AS MON, COUNT(1) AS TBACKCOUNT FROM TBACK LEFT JOIN FINISHMAINT ON TBACK.FRAMENO=FINISHMAINT.FRAMENO
WHERE DATE(FINISHMAINT.DELIVEREDDATE) >= TBACK.EXPECTSTARTDATE-7 DAYS AND DATE(FINISHMAINT.DELIVEREDDATE) <= TBACK.EXPECTSTARTDATE +30 DAYS
GROUP BY MONTH(TBACK.EXPECTSTARTDATE + 1 MONTH)
------------------------------------------------- WITH FINISHMAINT (FRAMENO,DELIVEREDDATE) AS
(
SELECT DISTINCT RP.FRAMENO, MAX(PRC.DELIVEREDDATE) OVER(PARTITION BY RP.FRAMENO) FROM RT_REPAIR RP
LEFT JOIN RT_REPAIRPART P ON RP.REPAIRNO=P.REPAIRNO
LEFT JOIN RT_REPAIRPROCESS PRC ON RP.REPAIRNO=PRC.REPAIRNO
WHERE RP.STATUSCODE = ''AND VALUE(RP.DELETED,'') = ''
AND P.PARTNO LIKE '15400%' AND VALUE(P.ACTUALOUTQTY,0)-VALUE(P.RETURNEDQTY,0)>0
), TBACK(FRAMENO,EXPECTSTARTDATE,TASKID) AS
(
SELECT DEF.FRAMENO,DEF.EXPECTSTARTDATE,DEF.TASKID FROM RT_CALLTASKDEF DEF
WHERE DEF.EXPECTSTARTDATE >= '2014-12-01' AND DEF.EXPECTSTARTDATE<='2015-11-30'
), TIMEPERTASK (TASKID,CALLTIMES) AS
(
SELECT DEF.TASKID, COUNT(1) AS CALLTIMES
FROM RT_CALLTASKRECDT RECDT LEFT JOIN RT_CALLTASKDEF DEF ON RECDT.TASKID=DEF.TASKID
WHERE RECDT.ACTIONMETHOD='PH'
GROUP BY DEF.TASKID
), ABC(TASKID, EXPECTSTARTDATE) AS
(
SELECT TBACK.TASKID,TBACK.EXPECTSTARTDATE FROM TBACK LEFT JOIN FINISHMAINT ON TBACK.FRAMENO=FINISHMAINT.FRAMENO
WHERE DATE(FINISHMAINT.DELIVEREDDATE) >= TBACK.EXPECTSTARTDATE-7 DAYS AND DATE(FINISHMAINT.DELIVEREDDATE) <= TBACK.EXPECTSTARTDATE +30 DAYS
) SELECT MONTH(EXPECTSTARTDATE + 1 MONTH) AS MON, SUM(TIMEPERTASK.CALLTIMES) AS CALLTIMES FROM ABC INNER JOIN TIMEPERTASK ON ABC.TASKID=TIMEPERTASK.TASKID
GROUP BY MONTH(EXPECTSTARTDATE + 1 MONTH)