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(sCorp )!=0," AND corp_code IN ('"+ sCorp +"')", "")}
),
     base_info AS (
         SELECT
             LEFT(t1.subject_code,6) AS subject_code,
             SUM(t1.begin_debit_blc) AS "期初借方余额",
             SUM(t1.begin_credit_blc) AS "期初贷方余额",
             SUM(t1.credit_amt) AS "贷方发生额",
             SUM(t1.debit_amt) AS "借方发生额",
             SUM(IF(t1.acnt_mon = '${FORMAT(sStart_mon,"MM")}',t1.begin_debit_blc,0)) AS "期初借方余额_本月",
             SUM(IF(t1.acnt_mon = '${FORMAT(sStart_mon,"MM")}',t1.begin_credit_blc,0)) AS "期初贷方余额_本月",
             SUM(IF(t1.acnt_mon = '${FORMAT(sStart_mon,"MM")}',t1.credit_amt,0)) AS "贷方发生额_本月",
             SUM(IF(t1.acnt_mon = '${FORMAT(sStart_mon,"MM")}',t1.debit_amt,0)) AS "借方发生额_本月"
         FROM
             "dwr"."fact_fim_subject_balance" t1
                 JOIN corp t2 ON t1.corp_code = t2.corp_code
         WHERE
             t1.acnt_year = '${YEAR(sStart_mon)}'
           AND t1.acnt_mon <= '${FORMAT(sStart_mon,"MM")}'
           AND t1.subject_code LIKE '2221%'
         GROUP BY
             LEFT(subject_code,6)
     ),
     subject_result AS (
         SELECT
             t1.subject_code AS "科目编号",
             ABS(SUM("贷方发生额")) AS "应交",

             ABS(SUM("贷方发生额_本月")) AS "应交_本月"
         FROM
             base_info t1
         WHERE
             t1.subject_code IN ('222102','222105','222104','222113','222114','222106','222107','222110','222108','222109','222112','222118','222125','222121')
         GROUP BY
             t1.subject_code

         UNION ALL

         SELECT
             '其他税' AS "科目编号",

             ABS(SUM(t1."贷方发生额") - SUM(IF(t1.subject_code IN ('222101','222102','222104','222105','222106','222107','222108','222109','222110','222112','222113','222114','222116','2221121','222123','222125'),t1."贷方发生额",0))) AS "应交",

             ABS(SUM(t1."贷方发生额_本月") - SUM(IF(t1.subject_code IN ('222101','222102','222104','222105','222106','222107','222108','222109','222110','222112','222113','222114','222116','222121','222123','222125'),t1."贷方发生额_本月",0))) AS "应交_本月"

         FROM
             base_info t1

         UNION ALL

         SELECT
             '增值税' AS "科目编号",
             ABS(SUM(IF(LEFT(t1.subject_code,8) IN ('22210105','22210116','22210117'),t1.credit_amt,0)) + SUM(IF(LEFT(t1.subject_code,6) IN ('222123'),t1.credit_amt,0)) - SUM(IF(LEFT(t1.subject_code,8) IN ('22210101','22210104','22210108','22210110','22210112','22210118'),t1.credit_amt,0))) AS "应交",

             ABS(SUM(IF(LEFT(t1.subject_code,8) IN ('22210105','22210116','22210117','222123') AND t1.acnt_mon = '${FORMAT(sStart_mon,"MM")}',t1.credit_amt,0)) + SUM(IF(LEFT(t1.subject_code,6) IN ('222123') AND t1.acnt_mon = '${FORMAT(sStart_mon,"MM")}',t1.credit_amt,0)) - SUM(IF(LEFT(t1.subject_code,8) IN ('22210101','22210104','22210108','22210110','22210112','22210118') AND t1.acnt_mon = '${FORMAT(sStart_mon,"MM")}',t1.debit_amt,0))) AS "应交_本月"
         FROM
             "dwr"."fact_fim_subject_balance" t1
                 JOIN corp t2 ON t1.corp_code = t2.corp_code
         WHERE
             t1.acnt_year = '${YEAR(sStart_mon)}'
           AND t1.acnt_mon <= '${FORMAT(sStart_mon,"MM")}'
           AND t1.subject_code LIKE '2221%'

         UNION ALL

         SELECT
             '营业收入' AS "科目编号",
             SUM(t1.credit_amt) AS "应交",
             SUM(IF(t1.acnt_mon = '${FORMAT(sStart_mon,"MM")}',t1.credit_amt,0)) AS "应交_本月"
         FROM
             "dwr"."fact_fim_subject_balance" t1
                 JOIN corp t2 ON t1.corp_code = t2.corp_code
         WHERE
             t1.acnt_year = '${YEAR(sStart_mon)}'
           AND t1.acnt_mon <= '${FORMAT(sStart_mon,"MM")}'
           AND LEFT(t1.subject_code,4) IN ('6001','6051')
     ),
-- 同期
     base_info_tq AS (
         SELECT
             LEFT(t1.subject_code,6) AS subject_code,
             SUM(t1.begin_debit_blc) AS "期初借方余额",
             SUM(t1.begin_credit_blc) AS "期初贷方余额",
             SUM(t1.credit_amt) AS "贷方发生额",
             SUM(t1.debit_amt) AS "借方发生额",
             SUM(IF(t1.acnt_mon = '${FORMAT(sStart_mon,"MM")}',t1.begin_debit_blc,0)) AS "期初借方余额_本月",
             SUM(IF(t1.acnt_mon = '${FORMAT(sStart_mon,"MM")}',t1.begin_credit_blc,0)) AS "期初贷方余额_本月",
             SUM(IF(t1.acnt_mon = '${FORMAT(sStart_mon,"MM")}',t1.credit_amt,0)) AS "贷方发生额_本月",
             SUM(IF(t1.acnt_mon = '${FORMAT(sStart_mon,"MM")}',t1.debit_amt,0)) AS "借方发生额_本月"
         FROM
             "dwr"."fact_fim_subject_balance" t1
                 JOIN corp t2 ON t1.corp_code = t2.corp_code
         WHERE
             t1.acnt_year = '${YEAR(sStart_mon) - 1}'
           AND t1.acnt_mon <= '${FORMAT(sStart_mon,"MM")}'
           AND t1.subject_code LIKE '2221%'
         GROUP BY
             LEFT(subject_code,6)
     ),
     subject_result_tq AS (
         SELECT
             t1.subject_code AS "科目编号",
             ABS(SUM("贷方发生额")) AS "应交_同期",

             ABS(SUM("贷方发生额_本月")) AS "应交_本月_同期"
         FROM
             base_info t1
         WHERE
             t1.subject_code IN ('222102','222105','222104','222113','222114','222106','222107','222110','222108','222109','222112','222118','222125','222121')
         GROUP BY
             t1.subject_code

         UNION ALL

         SELECT
             '其他税' AS "科目编号",
             ABS(SUM(t1."贷方发生额") - SUM(IF(t1.subject_code IN ('222101','222102','222104','222105','222106','222107','222108','222109','222110','222112','222113','222114','222116','2221121','222123','222125'),t1."贷方发生额",0))) AS "应交_同期",

             ABS(SUM(t1."贷方发生额_本月") - SUM(IF(t1.subject_code IN ('222101','222102','222104','222105','222106','222107','222108','222109','222110','222112','222113','222114','222116','222121','222123','222125'),t1."贷方发生额_本月",0))) AS "应交_本月_同期"

         FROM
             base_info t1

         UNION ALL

         SELECT
             '增值税' AS "科目编号",
             ABS(SUM(IF(LEFT(t1.subject_code,8) IN ('22210105','22210116','22210117'),t1.credit_amt,0)) + SUM(IF(LEFT(t1.subject_code,6) IN ('222123'),t1.credit_amt,0)) - SUM(IF(LEFT(t1.subject_code,8) IN ('22210101','22210104','22210108','22210110','22210112','22210118'),t1.credit_amt,0))) AS "应交_同期",

             ABS(SUM(IF(LEFT(t1.subject_code,8) IN ('22210105','22210116','22210117') AND t1.acnt_mon = '${FORMAT(sStart_mon,"MM")}',t1.credit_amt,0)) + SUM(IF(LEFT(t1.subject_code,6) IN ('222123') AND t1.acnt_mon = '${FORMAT(sStart_mon,"MM")}',t1.credit_amt,0)) - SUM(IF(LEFT(t1.subject_code,8) IN ('22210101','22210104','22210108','22210110','22210112','22210118') AND t1.acnt_mon = '${FORMAT(sStart_mon,"MM")}',t1.debit_amt,0))) AS "应交_本月_同期"
         FROM
             "dwr"."fact_fim_subject_balance" t1
                 JOIN corp t2 ON t1.corp_code = t2.corp_code
         WHERE
             t1.acnt_year = '${YEAR(sStart_mon) - 1}'
           AND t1.acnt_mon <= '${FORMAT(sStart_mon,"MM")}'
           AND t1.subject_code LIKE '2221%'

         UNION ALL

         SELECT
             '营业收入' AS "科目编号",
             SUM(t1.credit_amt) AS "应交_同期",
             SUM(IF(t1.acnt_mon = '${FORMAT(sStart_mon,"MM")}',t1.credit_amt,0)) AS "应交_本月_同期"
         FROM
             "dwr"."fact_fim_subject_balance" t1
                 JOIN corp t2 ON t1.corp_code = t2.corp_code
         WHERE
             t1.acnt_year = '${YEAR(sStart_mon) - 1}'
           AND t1.acnt_mon <= '${FORMAT(sStart_mon,"MM")}'
           AND LEFT(t1.subject_code,4) IN ('6001','6051')
     )

SELECT
    fin0."税种",
    IFNULL(SUM(fin1."应交") / 10000,0)  AS "应交",
    IFNULL(SUM(fin1."应交_本月") / 10000,0)  AS "应交_本月",
    IFNULL(SUM(fin2."应交_同期") / 10000,0)  AS "应交_同期",
    IFNULL(SUM(fin2."应交_本月_同期") / 10000,0)  AS "应交_本月_同期"
FROM
    ( SELECT '增值税' AS "科目编号",'增值税' AS "税种"
      UNION ALL
      SELECT '222102' AS "科目编号",'消费税' AS "税种"
      UNION ALL
      SELECT '222105' AS "科目编号",'企业所得税' AS "税种"
      UNION ALL
      SELECT '222104' AS "科目编号",'城建及附加' AS "税种"
      UNION ALL
      SELECT '222113' AS "科目编号",'城建及附加' AS "税种"
      UNION ALL
      SELECT '222114' AS "科目编号",'城建及附加' AS "税种"
      UNION ALL
      SELECT '222106' AS "科目编号",'个人所得税' AS "税种"
      UNION ALL
      SELECT '222107' AS "科目编号",'其他税费' AS "税种"
      UNION ALL
      SELECT '222110' AS "科目编号",'其他税费' AS "税种"
      UNION ALL
      SELECT '222108' AS "科目编号",'其他税费' AS "税种"
      UNION ALL
      SELECT '222109' AS "科目编号",'其他税费' AS "税种"
      UNION ALL
      SELECT '222112' AS "科目编号",'其他税费' AS "税种"
      UNION ALL
      SELECT '222118' AS "科目编号",'其他税费' AS "税种"
      UNION ALL
      SELECT '222125' AS "科目编号",'其他税费' AS "税种"
      UNION ALL
      SELECT '222121' AS "科目编号",'其他税费' AS "税种"
      UNION ALL
      SELECT '其他税' AS "科目编号",'其他税费' AS "税种"
      UNION ALL
      SELECT '营业收入' AS "科目编号",'营业收入' AS "税种"
      UNION ALL
      SELECT '利润总额' AS "科目编号",'利润总额' AS "税种"
    ) fin0
        LEFT JOIN	subject_result fin1 ON fin0."科目编号" = fin1."科目编号"
        LEFT JOIN subject_result_tq fin2 ON fin0."科目编号" = fin2."科目编号"
GROUP BY
    fin0."税种"
ORDER BY
    DECODE(fin0."税种",'营业收入',1,'利润总额',2,'增值税',3,'消费税',4,'企业所得税',5,'城建及附加',6,'个人所得税',7,'其他税费',8)