WITH corp AS (
    SELECT
        corp_code -- 公司编码
         ,corp_name  -- 公司名称
    FROM dwr.dim_corp a
    WHERE corp_type = '实体'
      AND	(parent_corp_code LIKE '02%' OR 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 = a.corp_code
			)
)
        ${IF(LEN(sCorp)!=0," AND corp_code IN ('"+ sCorp +"')", "")}
),
     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(sStart_mon,"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 '6603%'
           AND A.acnt_year = '${YEAR(sStart_mon)}'
           AND A.acnt_mon <= '${FORMAT(sStart_mon,"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(sStart_mon,"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 '6603%'
           AND A.acnt_year = '${YEAR(sStart_mon) - 1}'
           AND A.acnt_mon <= '${FORMAT(sStart_mon,"MM")}'
         GROUP BY  A.subject_code
     ),
     subject_result AS (
         -- 本期统计
         SELECT
             '利息支出'          AS subject_name  -- 科目名称
              ,'660301'         AS subject_code -- 科目编码
              ,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 '660301%'
         UNION ALL
         SELECT
             '减：利息收入'          AS subject_name  -- 科目名称
              ,'660302'         AS subject_code -- 科目编码
              ,SUM(debit_amt_m) * -1  AS debit_amt_m  -- 借方金额_当月累计
              ,SUM(debit_amt_m) * -1  AS debit_amt_y  -- 借方金额_当年累计
              ,0                AS debit_amt_last_m  -- 借方金额_上年同期
              ,0                AS debit_amt_last_y  -- 借方金额_上年累计
         FROM base_info_curr
         WHERE subject_code LIKE '660302%'
         UNION ALL
         SELECT
             '汇兑损益'          AS subject_name  -- 科目名称
              ,'660303'         AS subject_code -- 科目编码
              ,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 '660303%'
         UNION ALL
         SELECT
             '金融机构手续费'          AS subject_name  -- 科目名称
              ,'660304'         AS subject_code -- 科目编码
              ,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 '660304%'
       -- UNION ALL
       -- SELECT
       --     '其他'          AS subject_name  -- 科目名称
       --      ,SUM(CASE WHEN subject_code LIKE '6603%' THEN debit_amt_m ELSE 0 END)
       --       - SUM(CASE WHEN subject_code LIKE '660301%' THEN debit_amt_m ELSE 0 END)
       --      - SUM(CASE WHEN subject_code LIKE '660303%' THEN debit_amt_m ELSE 0 END)
       --     - SUM(CASE WHEN subject_code LIKE '660304%' THEN debit_amt_m ELSE 0 END)
       --     + SUM(CASE WHEN subject_code LIKE '660302%' THEN debit_amt_m ELSE 0 END)
       --     AS debit_amt_m  -- 借方金额_当月累计
       --      ,SUM(CASE WHEN subject_code LIKE '6603%' THEN debit_amt_y ELSE 0 END)
       --           - SUM(CASE WHEN subject_code LIKE '660301%' THEN debit_amt_y ELSE 0 END)
       --           - SUM(CASE WHEN subject_code LIKE '660303%' THEN debit_amt_y ELSE 0 END)
       --           - SUM(CASE WHEN subject_code LIKE '660304%' THEN debit_amt_y ELSE 0 END)
       --     + SUM(CASE WHEN subject_code LIKE '660302%' THEN debit_amt_y ELSE 0 END)

       --          AS debit_amt_y  -- 借方金额_当年累计
       --      ,0                AS debit_amt_last_m  -- 借方金额_上年同期
       --      ,0                AS debit_amt_last_y  -- 借方金额_上年累计
       -- FROM base_info_curr
       -- WHERE subject_code like '6603%'
         UNION ALL
         -- 上期统计
         SELECT
             '利息支出'          AS subject_name  -- 科目名称
              ,'660301'         AS subject_code -- 科目编码
              ,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 '660301%'
         UNION ALL
         SELECT
             '减：利息收入'          AS subject_name  -- 科目名称
              ,'660302'         AS subject_code -- 科目编码
              ,0                AS debit_amt_m  -- 借方金额_当月累计
              ,0                AS debit_amt_y  -- 借方金额_当年累计
              ,SUM(debit_amt_m) * -1  AS debit_amt_last_m  -- 借方金额_上年同期
              ,SUM(debit_amt_y) * -1 AS debit_amt_last_y  -- 借方金额_上年累计
         FROM base_info_last
         WHERE subject_code LIKE '660302%'
         UNION ALL
         SELECT
             '汇兑损益'          AS subject_name  -- 科目名称
              ,'660303'         AS subject_code -- 科目编码
              ,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 '660303%'
         UNION ALL
         SELECT
             '金融机构手续费'          AS subject_name  -- 科目名称
              ,'660304'         AS subject_code -- 科目编码
              ,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 '660304%'
        -- UNION ALL
        -- SELECT
        --     '其他'          AS subject_name  -- 科目名称
        --      ,0            AS debit_amt_m  -- 借方金额_当月累计
        --      ,0            AS debit_amt_y  -- 借方金额_当年累计
        --      ,SUM(CASE WHEN subject_code LIKE '6603%' THEN debit_amt_m ELSE 0 END)
        --     - SUM(CASE WHEN subject_code LIKE '660301%' THEN debit_amt_m ELSE 0 END)
        --     - SUM(CASE WHEN subject_code LIKE '660303%' THEN debit_amt_m ELSE 0 END)
        --     - SUM(CASE WHEN subject_code LIKE '660304%' THEN debit_amt_m ELSE 0 END)
        --     + SUM(CASE WHEN subject_code LIKE '660302%' THEN debit_amt_m ELSE 0 END)
        --     AS debit_amt_last_m  -- 借方金额_上年同期
        --      ,SUM(CASE WHEN subject_code LIKE '6603%' THEN debit_amt_y ELSE 0 END)
        --           - SUM(CASE WHEN subject_code LIKE '660301%' THEN debit_amt_y ELSE 0 END)
        --           - SUM(CASE WHEN subject_code LIKE '660303%' THEN debit_amt_y ELSE 0 END)
        --           - SUM(CASE WHEN subject_code LIKE '660304%' THEN debit_amt_y ELSE 0 END)
        --     + SUM(CASE WHEN subject_code LIKE '660302%' THEN debit_amt_y ELSE 0 END)
--
        --     AS debit_amt_last_y  -- 借方金额_上年累计
--
        -- FROM base_info_last
        -- WHERE subject_code like '6603%'
)
SELECT
 subject_name
,subject_code
,debit_amt_m  -- 借方金额_当月累计
,debit_amt_y  -- 借方金额_当年累计
,debit_amt_last_m  -- 借方金额_上年同期
,debit_amt_last_y  -- 借方金额_上年累计
FROM
(
SELECT
    subject_name   AS subject_name  -- 科目名称
     ,subject_code  AS subject_code -- 科目编码
     ,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,subject_code
-- ORDER BY
--    DECODE(subject_name,'利息支出','a','减：利息收入','b','汇兑损益','c','金融机构手续费','d','其他','e')
UNION ALL
SELECT
    '合计'   AS subject_name  -- 科目名称
     ,'000000'  AS subject_code -- 科目编码
     ,IFNULL(SUM(CASE WHEN subject_name = '减：利息收入' THEN -1 * debit_amt_m ELSE  debit_amt_m END) / 10000,0)  AS debit_amt_m  -- 借方金额_当月累计
     ,IFNULL(SUM(CASE WHEN subject_name = '减：利息收入' THEN -1 * debit_amt_y  ELSE debit_amt_y  END) / 10000,0) AS debit_amt_y  -- 借方金额_当年累计
     ,IFNULL(SUM(CASE WHEN subject_name = '减：利息收入' THEN -1 * debit_amt_last_m ELSE debit_amt_last_m END ) / 10000,0) AS debit_amt_last_m  -- 借方金额_上年同期
     ,IFNULL(SUM(CASE WHEN subject_name = '减：利息收入' THEN -1 * debit_amt_last_y ELSE debit_amt_last_y END) / 10000,0) AS debit_amt_last_y  -- 借方金额_上年累计
FROM subject_result
)t

ORDER BY
    DECODE(subject_name,'利息支出','a','减：利息收入','b','汇兑损益','c','金融机构手续费','d','其他','e')



select
    stat_date,--年月，日期格式
    corp_code,--公司编码
    tax_name, --税种
    actual_amt, --实际入库
    refund_amt --退税金额
from dwi.dwi_fim_tax_payed
WHERE LEFT(stat_date,7) = '2024-10'
  AND corp_code = '0106'
