WITH corp AS (
    SELECT
        t1.corp_code,
        t1.corp_name,
        t2.tax_name
    FROM
        "dwr"."dim_corp" t1
            JOIN "sdi_hzg"."ipt_corp_tax_relation" t2 ON t1.corp_code = t2.corp_code
    WHERE
        t1.corp_type = '实体'
      AND	(t1.parent_corp_code LIKE '02%' OR t1.corp_code = '0301')
      AND  exists (
        select
        1
from
	dm.dm_bi_user_permisson
where
	bi_user = '${fine_username}'
	and per_model IN ('all','sal')
	and (
			is_all = 1
			OR per_code = t1.corp_code
			)
)
        ${IF(LEN(sCorp )!=0," AND t1.corp_code IN ('"+ sCorp +"')", "")}

),
     subject AS (
         SELECT 1 AS seq, '增值税' AS "科目编号",'增值税' AS "税种" ,'应交税金' AS "类别", 1 AS class_
         UNION ALL
         SELECT 2 AS seq, '222102' AS "科目编号",'消费税' AS "税种" ,'应交税金' AS "类别", 2 AS class_
         UNION ALL
         SELECT 3 AS seq, '222105' AS "科目编号",'企业所得税' AS "税种" ,'应交税金' AS "类别", 2 AS class_
         UNION ALL
         SELECT 4 AS seq, '222104' AS "科目编号",'城建' AS "税种" ,'应交税金' AS "类别", 2 AS class_
         UNION ALL
         SELECT 5 AS seq, '222113' AS "科目编号",'附加税' AS "税种" ,'应交税金' AS "类别", 2 AS class_
         UNION ALL
         SELECT 6 AS seq, '222114' AS "科目编号",'附加税' AS "税种" ,'应交税金' AS "类别", 2 AS class_
         UNION ALL
         SELECT 7 AS seq, '222106' AS "科目编号",'个税' AS "税种" ,'应交税金' AS "类别", 2 AS class_
         UNION ALL
         SELECT 8 AS seq, '222107' AS "科目编号",'房土税' AS "税种" ,'应交税金' AS "类别", 2 AS class_
         UNION ALL
         SELECT 9 AS seq, '222110' AS "科目编号",'房土税' AS "税种" ,'应交税金' AS "类别", 2 AS class_
         UNION ALL
         SELECT 10 AS seq, '其他税' AS "科目编号",'其他税' AS "税种" ,'应交税金' AS "类别", 1 AS class_
     ),
     base_info AS (
         SELECT
             t1.corp_code,
             t1.acnt_mon,
             LEFT(t1.subject_code,6) AS subject_code,
             SUM(t1.credit_amt) AS credit_amt
         FROM
             "dwr"."fact_fim_subject_balance" t1
                 JOIN corp t2 ON t1.corp_code = t2.corp_code
         WHERE t1.acnt_year = '${left(sStart_mon,4)}'
           AND t1.acnt_mon = '${right(sStart_mon,2)}'
           AND t1.subject_code LIKE '2221%'
         GROUP BY
             LEFT(t1.subject_code,6),
             t1.corp_code,
             t1.acnt_mon
     ),

     subject_result AS (
         SELECT
             t2.corp_code,
             t1."类别",
             t1."税种",
             SUM(t2.credit_amt) AS "本年累计",
             SUM(IF(t2.acnt_mon = '${right(sStart_mon,2)}',t2.credit_amt,0)) AS "本月"
         FROM
             subject t1
                 LEFT JOIN	base_info t2 ON t1."科目编号" = t2.subject_code
         WHERE
             t1.class_ = 2
         GROUP BY
             t2.corp_code,
             t1."类别",
             t1."税种"

         UNION ALL

         SELECT
             t1.corp_code,
             '应交税金' AS "类别",
             '其他税' AS "税种",
             SUM(t1.credit_amt) - SUM(IF(t1.subject_code IN ('222101','222102','222104','222105','222106','222107','222110','222113','222114','222116','222123'),t1.credit_amt,0)) AS "本年累计",
             SUM(IF(t1.acnt_mon = '${right(sStart_mon,2)}',t1.credit_amt,0)) - SUM(IF(t1.subject_code IN ('222101','222102','222104','222105','222106','222107','222110','222113','222114','222116','222123') AND t1.acnt_mon = '${right(sStart_mon,2)}',t1.credit_amt,0)) AS "本月"
         FROM
             base_info t1
         GROUP BY
             t1.corp_code

         UNION ALL

         SELECT
             t1.corp_code,
             '应交税金' AS "类别",
             '增值税' 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 = '${right(sStart_mon,2)}',t1.credit_amt,0)) + SUM(IF(LEFT(t1.subject_code,6) IN ('222123') AND t1.acnt_mon = '${right(sStart_mon,2)}',t1.credit_amt,0)) - SUM(IF(LEFT(t1.subject_code,8) IN ('22210101','22210104','22210108','22210110','22210112','22210118') AND t1.acnt_mon = '${right(sStart_mon,2)}',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 = '${left(sStart_mon,4)}'
           AND t1.acnt_mon = '${right(sStart_mon,2)}'
           AND t1.subject_code LIKE '2221%'
         GROUP BY
             t1.corp_code
     )

SELECT
    fin0.corp_code,
    fin0.corp_name,
    fin1."税种",
    IFNULL(fin2."本年累计",0)  / 10000 AS "本年累计",
    IFNULL(fin2."本月",0) / 10000  AS "本月"
FROM
    corp fin0
        LEFT JOIN
    (
        SELECT
            "税种",
            "类别",
            MAX(seq) AS seq
        FROM
            subject
        GROUP BY
            "税种",
            "类别"
    ) fin1 ON 1=1
        LEFT JOIN subject_result fin2 ON fin0.corp_code = fin2.corp_code AND fin1."税种" = fin2."税种"
ORDER BY
    fin1.seq,
    fin0.corp_code