WITH corp AS (
    SELECT
        corp_code -- 公司编码
         ,corp_name  -- 公司名称
    FROM dwr.dim_corp
    WHERE corp_type = '实体'
      AND	(parent_corp_code LIKE '02%' OR corp_code = '0301')
        ${IF(LEN(sCorpCode)!=0," AND corp_code IN ('"+JOINARRAY(sCorpCode,"','")+"')", "")}
),
     base_info_curr AS (
         SELECT
             A.subject_code  AS subject_code      -- 科目编码
              ,SUM(A.debit_amt) 	   AS debit_amt_y         -- 借方金额_当年累计
              ,SUM(IF(A.acnt_mon = '${FORMAT(sQueryDate,"MM")}',A.debit_amt,0)) 	 AS debit_amt_m  -- 借方金额_当月累计
         FROM dwr.fact_fim_subject_balance A-- 科目余额
                  JOIN corp B ON A.corp_code = B.corp_code
         WHERE A.subject_code like '6604%'
           AND A.acnt_year = '${YEAR(sQueryDate)}'
           AND A.acnt_mon <= '${FORMAT(sQueryDate,"MM")}'
         GROUP BY  A.subject_code
     ),
     base_info_last AS (
         SELECT
             A.subject_code  AS subject_code      -- 科目编码
              ,SUM(A.debit_amt) 	   AS debit_amt_y         -- 借方金额_当年累计
              ,SUM(IF(A.acnt_mon = '${FORMAT(sQueryDate,"MM")}',A.debit_amt,0)) 	 AS debit_amt_m  -- 借方金额_当月累计
         FROM dwr.fact_fim_subject_balance A-- 科目余额
                  JOIN corp B ON A.corp_code = B.corp_code
         WHERE A.subject_code like '6604%'
           AND A.acnt_year = '${YEAR(sQueryDate) - 1}'
           AND A.acnt_mon <= '${FORMAT(sQueryDate,"MM")}'
         GROUP BY  A.subject_code
     ),
     subject_result AS (
         -- 本期统计
         SELECT
             '材料费'          AS subject_name  -- 科目名称
              ,SUM(debit_amt_m) AS debit_amt_m  -- 借方金额_当月累计
              ,SUM(debit_amt_y) AS debit_amt_y  -- 借方金额_当年累计
              ,0                AS debit_amt_last_m  -- 借方金额_上年同期
              ,0                AS debit_amt_last_y  -- 借方金额_上年累计
         FROM base_info_curr
         WHERE subject_code like '660401%'
         UNION ALL
         SELECT
             '职工薪酬'          AS subject_name  -- 科目名称
              ,SUM(debit_amt_m) AS debit_amt_m  -- 借方金额_当月累计
              ,SUM(debit_amt_y) AS debit_amt_y  -- 借方金额_当年累计
              ,0                AS debit_amt_last_m  -- 借方金额_上年同期
              ,0                AS debit_amt_last_y  -- 借方金额_上年累计
         FROM base_info_curr
         WHERE subject_code like '660403%'
         UNION ALL
         SELECT
             '差旅费'          AS subject_name  -- 科目名称
              ,SUM(debit_amt_m) AS debit_amt_m  -- 借方金额_当月累计
              ,SUM(debit_amt_y) AS debit_amt_y  -- 借方金额_当年累计
              ,0                AS debit_amt_last_m  -- 借方金额_上年同期
              ,0                AS debit_amt_last_y  -- 借方金额_上年累计
         FROM base_info_curr
         WHERE subject_code like '660409%'
         UNION ALL
         SELECT
             '加工费'          AS subject_name  -- 科目名称
              ,SUM(debit_amt_m) AS debit_amt_m  -- 借方金额_当月累计
              ,SUM(debit_amt_y) AS debit_amt_y  -- 借方金额_当年累计
              ,0                AS debit_amt_last_m  -- 借方金额_上年同期
              ,0                AS debit_amt_last_y  -- 借方金额_上年累计
         FROM base_info_curr
         WHERE subject_code like '660408%'
         UNION ALL
         SELECT
             '技术服务费'          AS subject_name  -- 科目名称
              ,SUM(debit_amt_m) AS debit_amt_m  -- 借方金额_当月累计
              ,SUM(debit_amt_y) AS debit_amt_y  -- 借方金额_当年累计
              ,0                AS debit_amt_last_m  -- 借方金额_上年同期
              ,0                AS debit_amt_last_y  -- 借方金额_上年累计
         FROM base_info_curr
         WHERE subject_code like '660402%'
         UNION ALL
         SELECT
             '其他'          AS subject_name  -- 科目名称
              ,SUM(debit_amt_m) AS debit_amt_m  -- 借方金额_当月累计
              ,SUM(debit_amt_y) AS debit_amt_y  -- 借方金额_当年累计
              ,0                AS debit_amt_last_m  -- 借方金额_上年同期
              ,0                AS debit_amt_last_y  -- 借方金额_上年累计
         FROM base_info_curr
         WHERE (subject_code NOT like '660401%'
                AND subject_code NOT like '660403%'
                AND subject_code NOT like '660409%'
                AND subject_code NOT like '660408%'
                AND subject_code NOT like '660402%')
         UNION ALL
         -- 上期统计
         SELECT
             '材料费'            AS subject_name  -- 科目名称
              ,0                AS debit_amt_m  -- 借方金额_当月累计
              ,0                AS debit_amt_y  -- 借方金额_当年累计
              ,SUM(debit_amt_m) AS debit_amt_last_m  -- 借方金额_上年同期
              ,SUM(debit_amt_y) AS debit_amt_last_y  -- 借方金额_上年累计
         FROM base_info_last
         WHERE subject_code like '660401%'
         UNION ALL
         SELECT
             '职工薪酬'          AS subject_name  -- 科目名称
              ,0                AS debit_amt_m  -- 借方金额_当月累计
              ,0                AS debit_amt_y  -- 借方金额_当年累计
              ,SUM(debit_amt_m) AS debit_amt_last_m  -- 借方金额_上年同期
              ,SUM(debit_amt_y) AS debit_amt_last_y  -- 借方金额_上年累计
         FROM base_info_last
         WHERE subject_code like '660403%'
         UNION ALL
         SELECT
             '差旅费'          AS subject_name  -- 科目名称
              ,0                AS debit_amt_m  -- 借方金额_当月累计
              ,0                AS debit_amt_y  -- 借方金额_当年累计
              ,SUM(debit_amt_m) AS debit_amt_last_m  -- 借方金额_上年同期
              ,SUM(debit_amt_y) AS debit_amt_last_y  -- 借方金额_上年累计
         FROM base_info_last
         WHERE subject_code like '660409%'
         UNION ALL
         SELECT
             '加工费'          AS subject_name  -- 科目名称
              ,0                AS debit_amt_m  -- 借方金额_当月累计
              ,0                AS debit_amt_y  -- 借方金额_当年累计
              ,SUM(debit_amt_m) AS debit_amt_last_m  -- 借方金额_上年同期
              ,SUM(debit_amt_y) AS debit_amt_last_y  -- 借方金额_上年累计
         FROM base_info_last
         WHERE subject_code like '660408%'
         UNION ALL
         SELECT
             '技术服务费'          AS subject_name  -- 科目名称
              ,0                AS debit_amt_m  -- 借方金额_当月累计
              ,0                AS debit_amt_y  -- 借方金额_当年累计
              ,SUM(debit_amt_m) AS debit_amt_last_m  -- 借方金额_上年同期
              ,SUM(debit_amt_y) AS debit_amt_last_y  -- 借方金额_上年累计
         FROM base_info_last
         WHERE subject_code like '660402%'
         UNION ALL
         SELECT
             '其他'          AS subject_name  -- 科目名称
              ,0                AS debit_amt_m  -- 借方金额_当月累计
              ,0                AS debit_amt_y  -- 借方金额_当年累计
              ,SUM(debit_amt_m) AS debit_amt_last_m  -- 借方金额_上年同期
              ,SUM(debit_amt_y) AS debit_amt_last_y  -- 借方金额_上年累计
         FROM base_info_last
         WHERE (subject_code NOT like '660401%'
             AND subject_code NOT like '660403%'
             AND subject_code NOT like '660409%'
             AND subject_code NOT like '660408%'
             AND subject_code NOT like '660402%')

     )
SELECT
    subject_name   AS subject_name  -- 科目名称
     ,IFNULL(SUM(debit_amt_m     ) / 10000,0) AS debit_amt_m  -- 借方金额_当月累计
     ,IFNULL(SUM(debit_amt_y     ) / 10000,0) AS debit_amt_y  -- 借方金额_当年累计
     ,IFNULL(SUM(debit_amt_last_m) / 10000,0) AS debit_amt_last_m  -- 借方金额_上年同期
     ,IFNULL(SUM(debit_amt_last_y) / 10000,0) AS debit_amt_last_y  -- 借方金额_上年累计
FROM subject_result
GROUP BY
    subject_name
ORDER BY
    DECODE(subject_name,'材料费','a','职工薪酬','b','差旅费','c','加工费','d','技术服务费','e','其他','f')