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 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(t1.begin_credit_blc) - SUM(t1.begin_debit_blc) AS "期初贷正借负"
         FROM
             "dwr"."fact_fim_subject_balance" t1
                 JOIN corp t2 ON t1.corp_code = t2.corp_code
         WHERE
             t1.acnt_year = '${YEAR(sQueryDate)}'
           AND t1.acnt_mon = '${FORMAT(sQueryDate,"MM")}'
           AND t1.subject_code LIKE '2221%'
         GROUP BY
             LEFT(subject_code,6)
     ),
     subject_result AS (
         SELECT
             t1.subject_code AS "科目编号",
             ABS(SUM(t1."期初贷正借负")) AS "期初余额",

             ABS(SUM(t1."贷方发生额")) 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','222121','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,6) IN ('222101','222116','222123'),t1.begin_credit_blc,0)) - SUM(IF(LEFT(t1.subject_code,6) IN ('222101','222116','222123'),t1.begin_debit_blc,0))) 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.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(sQueryDate)}'
           AND t1.acnt_mon = '${FORMAT(sQueryDate,"MM")}'
           AND t1.subject_code LIKE '2221%'
     )
SELECT
    fin0."税种",
    IFNULL(fin1."期初余额",0)  AS "期初余额",
    IFNULL(fin1."本期应交",0)  AS "本期应交"
FROM
    ( SELECT 1 AS seq, '增值税' AS "科目编号",'增值税' AS "税种"
      UNION ALL
      SELECT 2 AS seq, '222102' AS "科目编号",'消费税' AS "税种"
      UNION ALL
      SELECT 3 AS seq, '222105' AS "科目编号",'企业所得税' AS "税种"
      UNION ALL
      SELECT 4 AS seq, '222104' AS "科目编号",'城市维护建设税' AS "税种"
      UNION ALL
      SELECT 5 AS seq, '222113' AS "科目编号",'教育费附加' AS "税种"
      UNION ALL
      SELECT 6 AS seq, '222114' AS "科目编号",'地方教育费附加' AS "税种"
      UNION ALL
      SELECT 7 AS seq, '222106' AS "科目编号",'个人所得税' AS "税种"
      UNION ALL
      SELECT 8 AS seq, '222107' AS "科目编号",'房产税' AS "税种"
      UNION ALL
      SELECT 9 AS seq, '222110' AS "科目编号",'土地使用税' AS "税种"
      UNION ALL
      SELECT 10 AS seq, '222108' AS "科目编号",'车船税' AS "税种"
      UNION ALL
      SELECT 11 AS seq, '222109' AS "科目编号",'印花税' AS "税种"
      UNION ALL
      SELECT 12 AS seq, '222112' AS "科目编号",'资源税' AS "税种"
      UNION ALL
      SELECT 13 AS seq, '222118' AS "科目编号",'契税' AS "税种"
      UNION ALL
      SELECT 14 AS seq, '222125' AS "科目编号",'环境保护费' AS "税种"
      UNION ALL
      SELECT 15 AS seq, '222121' AS "科目编号",'文化事业建设费' AS "税种"
      UNION ALL
      SELECT 16 AS seq, '其他税' AS "科目编号",'其他税' AS "税种"
    ) fin0
        LEFT JOIN	subject_result fin1 ON fin0."科目编号" = fin1."科目编号"
ORDER BY
    fin0.seq