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 '6601%'
           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 '6601%'
           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  -- 科目名称
              ,'660114,660127'  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 '660114%' or subject_code like '660127%')
         UNION ALL
         SELECT
             '其中: 广告费'          AS subject_name  -- 科目名称
              ,'660114'         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 '660114%'
         UNION ALL
         SELECT
             '      业务宣传费'          AS subject_name  -- 科目名称
              ,'660127'         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 '660127%'
         UNION ALL
         SELECT
             '职工薪酬'          AS subject_name  -- 科目名称
              ,'660150'         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 '660150%'
         UNION ALL
         SELECT
             '差旅费'          AS subject_name  -- 科目名称
              ,'660115'         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 '660115%'
         UNION ALL
         SELECT
             '折旧费'          AS subject_name  -- 科目名称
              ,'660117'         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 '660117%'
         UNION ALL
         SELECT
             '电商运营费'          AS subject_name  -- 科目名称
              ,'660136'         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 '660136%'
         UNION ALL
         SELECT
             '房租费'          AS subject_name  -- 科目名称
              ,'660130'         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 '660130%'
         UNION ALL
         SELECT
             '会议费'          AS subject_name  -- 科目名称
              ,'660125'         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 '660125%'
         UNION ALL
         SELECT
             '业务招待费'          AS subject_name  -- 科目名称
              ,'660167'         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 '660167%'
         UNION ALL
         SELECT
             '仓储费'          AS subject_name  -- 科目名称
              ,'660121'         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 '660121%'
         UNION ALL
         SELECT
             '通讯费'          AS subject_name  -- 科目名称
              ,'660164'         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 '660164%'
         UNION ALL
         SELECT
             '其他'          AS subject_name  -- 科目名称
              ,'0000'         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 '6601%'
        AND (subject_code not like '660114%'
             AND subject_code not like '660127%'
             AND subject_code NOT like '660150%'
             AND subject_code NOT like '660115%'
             AND subject_code NOT like '660117%'
             AND subject_code NOT like '660136%'
             AND subject_code NOT like '660130%'
             AND subject_code NOT like '660125%'
             AND subject_code NOT like '660167%'
             AND subject_code NOT like '660121%'
             AND subject_code NOT like '660164%'
            )
         UNION ALL
         SELECT
             '合计'          AS subject_name  -- 科目名称
              ,'6601'         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 '6601%'
         UNION ALL
         -- 上期统计
         SELECT
             '广宣费'          AS subject_name  -- 科目名称
              ,'660114,660127'         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 '660114%' OR subject_code like '660127%')

         UNION ALL
         SELECT
             '其中: 广告费'          AS subject_name  -- 科目名称
              ,'660114'         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 '660114%'
         UNION ALL
         SELECT
             '      业务宣传费'          AS subject_name  -- 科目名称
              ,'660127'         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 '660127%'
         UNION ALL
         SELECT
             '职工薪酬'          AS subject_name  -- 科目名称
              ,'660150'         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 '660150%'
         UNION ALL
         SELECT
             '差旅费'          AS subject_name  -- 科目名称
              ,'660115'         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 '660115%'
         UNION ALL
         SELECT
             '折旧费'          AS subject_name  -- 科目名称
              ,'660117'         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 '660117%'
         UNION ALL
         SELECT
             '电商运营费'          AS subject_name  -- 科目名称
              ,'660136'         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 '660136%'
         UNION ALL
         SELECT
             '房租费'          AS subject_name  -- 科目名称
              ,'660130'         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 '660130%'
         UNION ALL
         SELECT
             '会议费'          AS subject_name  -- 科目名称
              ,'660125'         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 '660125%'
         UNION ALL
         SELECT
             '业务招待费'          AS subject_name  -- 科目名称
              ,'660167'         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 '660167%'
         UNION ALL
         SELECT
             '仓储费'          AS subject_name  -- 科目名称
              ,'660121'         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 '660121%'
         UNION ALL
         SELECT
             '通讯费'          AS subject_name  -- 科目名称
              ,'660164'         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 '660164%'
         UNION ALL
         SELECT
             '其他'          AS subject_name  -- 科目名称
              ,'0000'         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 '6601%'
           AND (subject_code NOT LIKE '660114%'
             AND subject_code NOT LIKE '660127%'
             AND subject_code NOT LIKE '660150%'
             AND subject_code NOT LIKE '660115%'
             AND subject_code NOT LIKE '660117%'
             AND subject_code NOT LIKE '660136%'
             AND subject_code NOT LIKE '660130%'
             AND subject_code NOT LIKE '660125%'
             AND subject_code NOT LIKE '660167%'
             AND subject_code NOT LIKE '660121%'
             AND subject_code NOT LIKE '660164%')
        UNION ALL
             SELECT
             '合计'          AS subject_name  -- 科目名称
            ,'6601'         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 '6601%'

     )
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','折旧费','f','电商运营费','g','房租费','h','会议费','i','业务招待费','j','仓储费','k','通讯费','l','其他','m','合计','n')