SELECT nvl(sum(t.QUANTITY),0) as QUANTITY,t.INSPSTATUSNAME,nvl(t1.QUANTITYt,0),t.INSPSTATUS, TO_CHAR(to_date(t.FDATE,'yyyy-mm-dd'),'yyyy-mm') as tdata, case t.INSPSTATUS when 'Unqualified' then '2' when 'Qualified' then '1' when 'Waiting' then '3' else '4' end as status, TO_CHAR(to_date(t.FDATE,'yyyy-mm-dd'),'mm') as month, TO_CHAR(to_date(t.FDATE,'yyyy-mm-dd'),'yyyy') as year, SYSDATE, nvl(t2.folt,0) as folt,nvl(t3.countfolt,0) as countfolt FROM WMS_STORAGELIST t LEFT JOIN ( select TO_CHAR(to_date(FDATE,'yyyy-mm-dd'),'yyyy-mm') as year_mon, sum(QUANTITY) as QUANTITYt,INSPSTATUSNAME FROM WMS_STORAGELIST GROUP BY TO_CHAR(to_date(FDATE,'yyyy-mm-dd'),'yyyy-mm'),INSPSTATUSNAME )t1 on t1.INSPSTATUSNAME=t.INSPSTATUSNAME and to_char(add_months(to_date(t.FDATE,'yyyy-mm-dd'),-12),'yyyy-mm')=t1.year_mon LEFT JOIN ( select COUNT(DISTINCT FACTORYLOTNO || WORKLOTNO) as folt,INSPSTATUSNAME,TO_CHAR(to_date(FDATE,'yyyy-mm-dd'),'yyyy-mm') as year_mon2 from WMS_STORAGELIST GROUP BY INSPSTATUSNAME,TO_CHAR(to_date(FDATE,'yyyy-mm-dd'),'yyyy-mm') ) t2 on t2.INSPSTATUSNAME=t.INSPSTATUSNAME and TO_CHAR(to_date(t.FDATE,'yyyy-mm-dd'),'yyyy-mm')=t2.year_mon2 LEFT JOIN ( select COUNT(DISTINCT FACTORYLOTNO || WORKLOTNO) as countfolt,INSPSTATUSNAME,TO_CHAR(to_date(FDATE,'yyyy-mm-dd'),'yyyy-mm') as year_mon2 from WMS_STORAGELIST GROUP BY INSPSTATUSNAME,TO_CHAR(to_date(FDATE,'yyyy-mm-dd'),'yyyy-mm') ) t3 on t3.INSPSTATUSNAME=t.INSPSTATUSNAME and to_char(add_months(to_date(t.FDATE,'yyyy-mm-dd'),-12),'yyyy-mm')=t3.year_mon2 where t.FDATE>='2021' GROUP BY t.INSPSTATUSNAME,TO_CHAR(to_date(t.FDATE,'yyyy-mm-dd'),'yyyy-mm'),TO_CHAR(to_date(t.FDATE,'yyyy-mm-dd'),'mm'), TO_CHAR(to_date(t.FDATE,'yyyy-mm-dd'),'yyyy') ,t1.QUANTITYt,t.INSPSTATUS,t2.folt,t3.countfolt ORDER BY TO_CHAR(to_date(t.FDATE,'yyyy-mm-dd'),'yyyy-mm') |