<?xml version="1.0" encoding="UTF-8"?>
<queryMap desc = "半成品收发存">
<query id = "FrmBcStoveStore.queryData" desc = "查询">
<![CDATA[
SELECT JUDGE_STOVE_NO,
       BATCH_NO,
       MATERIAL_NAME,
       PROCESS_CDOE,
       BELONG_CODE,
       (SELECT T.DEPARTNAME FROM CORE_APP_DEPARTMENT T WHERE T.DEPARTID = H.BELONG_CODE)BELONG_NAME,
       SUM(ORIGINAL_COUNT)ORIGINAL_COUNT,
       SUM(ORIGINAL_AMOUNT)ORIGINAL_AMOUNT,
       SUM(DAY_1COUNT)DAY_1COUNT,
       SUM(DAY_1)DAY_1,
       SUM(MONTH_1COUNT)MONTH_1COUNT,
       SUM(MONTH_1)MONTH_1,
       SUM(DAY_2COUNT)DAY_2COUNT,
       SUM(DAY_2)DAY_2,
       SUM(MONTH_2COUNT)MONTH_2COUNT,
       SUM(MONTH_2)MONTH_2,
       SUM((ORIGINAL_COUNT + MONTH_1COUNT - MONTH_2COUNT)) AS END_COUNT,
       SUM((ORIGINAL_AMOUNT + MONTH_1 - MONTH_2)) AS END_AMOUNT,
       SUM(MATCOUNT)MATCOUNT,
       SUM(MATWGT)MATWGT,
       NVL(SUM(NEXTMOTH_COUNT),'0')NEXTMOTH_COUNT,
       NVL(SUM(NEXTMOTH_WEIGHT),'0')NEXTMOTH_WEIGHT,
       NVL(SUM(CNEXTMOTH_COUNT),'0')CNEXTMOTH_COUNT,
       NVL(SUM(CNEXTMOTH_WEIGHT),'0')CNEXTMOTH_WEIGHT,
       SUM((ORIGINAL_COUNT + MONTH_1COUNT + NEXTMOTH_COUNT - MONTH_2COUNT- CNEXTMOTH_COUNT - MATCOUNT)) AS QUANTITYCOUNT,
       SUM((ORIGINAL_AMOUNT + MONTH_1 + NEXTMOTH_WEIGHT - MONTH_2 -CNEXTMOTH_WEIGHT - MATWGT)) AS QUANTITYWGT
  FROM (SELECT JUDGE_STOVE_NO,
               BATCH_NO,
               MATERIAL_NAME,
               PROCESS_CDOE,
               BELONG_CODE,
               SUM(K.ORIGINAL_COUNT) AS ORIGINAL_COUNT,
               SUM(K.ORIGINAL_AMOUNT) AS ORIGINAL_AMOUNT,
               NVL(SUM(CASE
                         WHEN TRUNC(K.INSTOCK_TIME) = TO_DATE(?, 'YYYYMMDD') THEN
                          NVL(K.INLISTCOUNT, 0)
                       END),
                   0) DAY_1COUNT,
               NVL(SUM(CASE
                         WHEN TRUNC(K.INSTOCK_TIME) = TO_DATE(?, 'YYYYMMDD') THEN
                          NVL(K.INLISTWGT, 0)
                       END),
                   0) DAY_1,
               NVL(SUM(CASE
                         WHEN K.BAL_YEAR_MONTH >= ? AND
                              TRUNC(K.INSTOCK_TIME) <=
                              TO_DATE(?, 'YYYYMMDD') THEN
                          NVL(K.INLISTCOUNT, 0)
                       END),
                   0) MONTH_1COUNT,
               NVL(SUM(CASE
                         WHEN K.BAL_YEAR_MONTH >= ? AND
                              TRUNC(K.INSTOCK_TIME) <=
                              TO_DATE(?, 'YYYYMMDD') THEN
                          NVL(K.INLISTWGT, 0)
                       END),
                   0) MONTH_1,
               NVL(SUM(CASE
                         WHEN TRUNC(K.OUTSTOCK_TIME) =
                              TO_DATE(?, 'YYYYMMDD') THEN
                          NVL(K.OUTLISTCOUNT, 0)
                       END),
                   0) DAY_2COUNT,
               NVL(SUM(CASE
                         WHEN TRUNC(K.OUTSTOCK_TIME) =
                              TO_DATE(?, 'YYYYMMDD') THEN
                          NVL(K.OUTLISTWGT, 0)
                       END),
                   0) DAY_2,
               NVL(SUM(CASE
                         WHEN K.BAL_YEAR_MONTH >= ? AND
                              TRUNC(K.OUTSTOCK_TIME) <=
                              TO_DATE(?, 'YYYYMMDD') THEN
                          NVL(K.OUTLISTCOUNT, 0)
                       END),
                   0) MONTH_2COUNT,
               NVL(SUM(CASE
                         WHEN K.BAL_YEAR_MONTH >= ? AND
                              TRUNC(K.OUTSTOCK_TIME) <=
                              TO_DATE(?, 'YYYYMMDD') THEN
                          NVL(K.OUTLISTWGT, 0)
                       END),
                   0) MONTH_2,
               SUM(K.MATCOUNT) AS MATCOUNT,
               SUM(K.MATWGT) AS MATWGT,
               SUM(NVL(K.NEXTMOTH_COUNT,'0'))AS NEXTMOTH_COUNT,
               SUM(NVL(K.NEXTMOTH_WEIGHT,'0'))AS NEXTMOTH_WEIGHT,
               SUM(NVL(K.CNEXTMOTH_COUNT,'0'))AS CNEXTMOTH_COUNT,
               SUM(NVL(K.CNEXTMOTH_WEIGHT,'0'))AS CNEXTMOTH_WEIGHT
          FROM (SELECT JUDGE_STOVE_NO,
                       BATCH_NO,
                       MATERIAL_NAME,
                       SUM(ACT_COUNT) ORIGINAL_COUNT,
                       SUM(ACT_WEIGHT) ORIGINAL_AMOUNT,
                       0 INLISTCOUNT,
                       0 INLISTWGT,
                       0 NEXTMOTH_COUNT,
                       0 NEXTMOTH_WEIGHT,
                       0 OUTLISTCOUNT,
                       0 OUTLISTWGT,
                       0 CNEXTMOTH_COUNT,
                       0 CNEXTMOTH_WEIGHT,
                       0 MATCOUNT,
                       0 MATWGT,
                       NULL OUTSTOCK_TIME,
                       NULL INSTOCK_TIME,
                       NULL BAL_YEAR_MONTH,
                       PROCESS_CDOE,
                       BELONG_CODE
                  FROM YDM_BC_INITIAL T
                 WHERE T.BAL_YEAR_MONTH = ?
                  AND T.BELONG_CODE  IN ('002001001','002023005')
                   #CONDTIONS#
                 GROUP BY T.JUDGE_STOVE_NO,T.BATCH_NO,T.MATERIAL_NAME,T.PLINE_CODE,T.BELONG_CODE,T.BELONG_NAME,T.PROCESS_CDOE
                UNION ALL
                SELECT JUDGE_STOVE_NO,
                       BATCH_NO,
                       MATERIAL_NAME,
                       0 ORIGINAL_COUNT,
                       0 ORIGINAL_AMOUNT,
                       ACT_COUNT INLISTCOUNT,
                       ACT_WEIGHT INLISTWGT,
                       0 NEXTMOTH_COUNT,
                       0 NEXTMOTH_WEIGHT,
                       0 OUTLISTCOUNT,
                       0 OUTLISTWGT,
                       0 CNEXTMOTH_COUNT,
                       0 CNEXTMOTH_WEIGHT,
                       0 MATCOUNT,
                       0 MATWGT,
                       NULL OUTSTOCK_TIME,
                       INSTOCK_TIME INSTOCK_TIME,
                       BAL_YEAR_MONTH,
                       PROCESS_CDOE,
                       BELONG_CODE
                  FROM YDM_BC_INLIST T
                 WHERE INSTOCK_TYPE_CODE IN('800709','800702','800711','800710','800706','800704') 
                   AND BAL_YEAR_MONTH >= ?
                   AND TRUNC(INSTOCK_TIME) <= TO_DATE(?, 'YYYYMMDD')
                   AND BELONG_CODE IN ('002001001','002023005')
                   #CONDTIONS#
                   UNION ALL
                SELECT JUDGE_STOVE_NO,
                       BATCH_NO,
                       MATERIAL_NAME,
                       0 ORIGINAL_COUNT,
                       0 ORIGINAL_AMOUNT,
                       0 INLISTCOUNT,
                       0 INLISTWGT,
                       ACT_COUNT NEXTMOTH_COUNT,
                       ACT_WEIGHT NEXTMOTH_WEIGHT,
                       0 OUTLISTCOUNT,
                       0 OUTLISTWGT,
                       0 CNEXTMOTH_COUNT,
                       0 CNEXTMOTH_WEIGHT,
                       0 MATCOUNT,
                       0 MATWGT,
                       NULL OUTSTOCK_TIME,
                       INSTOCK_TIME INSTOCK_TIME,
                       BAL_YEAR_MONTH,
                       PROCESS_CDOE,
                       BELONG_CODE
                  FROM YDM_BC_INLIST T
                 WHERE INSTOCK_TYPE_CODE IN('800709','800702','800711','800710','800706') 
                   AND TRUNC(INSTOCK_TIME) >= TO_DATE(?, 'YYYYMMDD')
                   --AND TRUNC(INSTOCK_TIME) <= TO_DATE(?, 'YYYYMMDD')
                   AND BELONG_CODE IN ('002001001','002023005')
                   #CONDTIONS#
                UNION ALL
                SELECT JUDGE_STOVE_NO,
                       BATCH_NO,
                       MATERIAL_NAME,
                       0 ORIGINAL_COUNT,
                       0 ORIGINAL_AMOUNT,
                       0 INLISTCOUNT,
                       0 INLISTWGT,
                       0 NEXTMOTH_COUNT,
                       0 NEXTMOTH_WEIGHT,
                       ACT_COUNT OUTLISTCOUNT,
                       ACT_WEIGHT OUTLISTWGT,
                       0 CNEXTMOTH_COUNT,
                       0 CNEXTMOTH_WEIGHT,
                       0 MATCOUNT,
                       0 MATWGT,
                       OUTSTOCK_TIME OUTSTOCK_TIME,
                       NULL INSTOCK_TIME,
                       BAL_YEAR_MONTH,
                       PROCESS_CDOE,
                       BELONG_CODE
                  FROM YDM_BC_OUTLIST T
                 WHERE OUTSTOCK_TYPE_CODE IN('800809','800802','800811','800808','800806','800804','800808')
                   AND BAL_YEAR_MONTH >= ?
                   AND TRUNC(OUTSTOCK_TIME) <= TO_DATE(?, 'YYYYMMDD')
                   AND BELONG_CODE IN ('002001001','002023005')
                   #CONDTIONS#
                   UNION ALL
                   SELECT JUDGE_STOVE_NO,
                       BATCH_NO,
                       MATERIAL_NAME,
                       0 ORIGINAL_COUNT,
                       0 ORIGINAL_AMOUNT,
                       0 INLISTCOUNT,
                       0 INLISTWGT,
                       0 NEXTMOTH_COUNT,
                       0 NEXTMOTH_WEIGHT,
                       0 OUTLISTCOUNT,
                       0 OUTLISTWGT,
                       ACT_COUNT CNEXTMOTH_COUNT,
                       (CASE
                             WHEN PONDER_NO IS NULL THEN
                              NVL(ACT_WEIGHT, 0)
                             ELSE
                              NVL(PONDER_GROSS_WT, 0)
                           END) CNEXTMOTH_WEIGHT,
                       0 MATCOUNT,
                       0 MATWGT,
                       OUTSTOCK_TIME OUTSTOCK_TIME,
                       NULL INSTOCK_TIME,
                       BAL_YEAR_MONTH,
                       PROCESS_CDOE,
                       BELONG_CODE
                  FROM YDM_BC_OUTLIST T
                 WHERE OUTSTOCK_TYPE_CODE IN('800809','800802','800811','800808','800806','800804','800808')
                   AND TRUNC(OUTSTOCK_TIME) >= TO_DATE(?, 'YYYYMMDD')
                   --AND TRUNC(OUTSTOCK_TIME) <= TO_DATE(?, 'YYYYMMDD')
                   AND BELONG_CODE IN ('002001001','002023005')
                   #CONDTIONS#
                UNION ALL
                SELECT F.JUDGE_STOVE_NO,
                       F.BATCH_NO,
                       F.MATERIAL_NAME,
                       0 ORIGINAL_COUNT,
                       0 ORIGINAL_AMOUNT,
                       0 INLISTCOUNT,
                       0 INLISTWGT,
                       0 NEXTMOTH_COUNT,
                       0 NEXTMOTH_WEIGHT,
                       0 OUTLISTCOUNT,
                       0 OUTLISTWGT,
                       0 CNEXTMOTH_COUNT,
                       0 CNEXTMOTH_WEIGHT,
                       SUM(F.ACT_COUNT) MATCOUNT,
                       SUM(F.ACT_WEIGHT) MATWGT,
                       NULL OUTSTOCK_TIME,
                       NULL INSTOCK_TIME,
                       NULL BAL_YEAR_MONTH,
                       F.PROCESS_CDOE,
                       F.BELONG_CODE
                  FROM MAT_BC_M F
                 WHERE (F.JUDGE_RESULT_CODE IN('40740701','40740702','40740706','40740709') OR ( F.INSTANCY_FLAG = '1'))
                 AND F.QCM_STAUS = '00'
                 --AND F.PLINE_CODE <> 'C095'
                 AND F.BELONG_CODE IN ('002001001','002023005')
                 AND F.PROCESS_CDOE <>'H'
                 #CONDTIONS#
                 GROUP BY F.JUDGE_STOVE_NO,F.BATCH_NO,F.MATERIAL_NAME,F.PROCESS_CDOE,F.BELONG_CODE) K
         GROUP BY K.JUDGE_STOVE_NO,K.BATCH_NO,K.MATERIAL_NAME,K.PROCESS_CDOE,K.BELONG_CODE) H
         GROUP BY JUDGE_STOVE_NO,BATCH_NO,MATERIAL_NAME,PROCESS_CDOE,BELONG_CODE
       ]]>
</query>
</queryMap>