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 '6602%' 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 '6602%' 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 -- 科目名称 ,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 '660297%' UNION ALL SELECT '折旧费' AS subject_name -- 科目名称 ,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 '660219%' UNION ALL SELECT '物料消耗' AS subject_name -- 科目名称 ,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 '660289%' UNION ALL SELECT '存货损耗' AS subject_name -- 科目名称 ,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 '660243%' UNION ALL SELECT '办公费' AS subject_name -- 科目名称 ,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 '660226%' UNION ALL SELECT '咨询服务费' AS subject_name -- 科目名称 ,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 '660234%' UNION ALL SELECT '无形资产摊销' AS subject_name -- 科目名称 ,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 '660221%' UNION ALL SELECT '差旅费' AS subject_name -- 科目名称 ,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 '660227%' UNION ALL SELECT '物业管理费' AS subject_name -- 科目名称 ,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 '660232%' UNION ALL SELECT '水电气费' AS subject_name -- 科目名称 ,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 '660233%' UNION ALL SELECT '车辆使用费' AS subject_name -- 科目名称 ,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 '660231%' UNION ALL SELECT '业务招待费' AS subject_name -- 科目名称 ,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 '660273%' UNION ALL SELECT '使用权资产摊销' AS subject_name -- 科目名称 ,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 '660290%' UNION ALL SELECT '租赁费' AS subject_name -- 科目名称 ,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 '660237%' UNION ALL SELECT '通讯费' AS subject_name -- 科目名称 ,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 '660287%' UNION ALL SELECT '会议费' AS subject_name -- 科目名称 ,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 '660228%' UNION ALL SELECT '其他' AS subject_name -- 科目名称 ,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 NOT LIKE '660297%' AND subject_code NOT LIKE '660219%' AND subject_code NOT LIKE '660289%' AND subject_code NOT LIKE '660243%' AND subject_code NOT LIKE '660226%' AND subject_code NOT LIKE '660234%' AND subject_code NOT LIKE '660221%' AND subject_code NOT LIKE '660227%' AND subject_code NOT LIKE '660232%' AND subject_code NOT LIKE '660233%' AND subject_code NOT LIKE '660231%' AND subject_code NOT LIKE '660273%' AND subject_code NOT LIKE '660290%' AND subject_code NOT LIKE '660237%' AND subject_code NOT LIKE '660287%' AND subject_code NOT LIKE '660228%' ) UNION ALL -- 上期统计 SELECT '职工薪酬' AS subject_name -- 科目名称 ,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 '660297%' UNION ALL SELECT '折旧费' AS subject_name -- 科目名称 ,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 '660219%' UNION ALL SELECT '物料消耗' AS subject_name -- 科目名称 ,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 '660289%' UNION ALL SELECT '存货损耗' AS subject_name -- 科目名称 ,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 '660243%' UNION ALL SELECT '办公费' AS subject_name -- 科目名称 ,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 '660226%' UNION ALL SELECT '咨询服务费' AS subject_name -- 科目名称 ,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 '660234%' UNION ALL SELECT '无形资产摊销' AS subject_name -- 科目名称 ,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 '660221%' UNION ALL SELECT '差旅费' AS subject_name -- 科目名称 ,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 '660227%' UNION ALL SELECT '物业管理费' AS subject_name -- 科目名称 ,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 '660232%' UNION ALL SELECT '水电气费' AS subject_name -- 科目名称 ,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 '660233%' UNION ALL SELECT '车辆使用费' AS subject_name -- 科目名称 ,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 '660231%' UNION ALL SELECT '业务招待费' AS subject_name -- 科目名称 ,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 '660273%' UNION ALL SELECT '使用权资产摊销' AS subject_name -- 科目名称 ,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 '660290%' UNION ALL SELECT '租赁费' AS subject_name -- 科目名称 ,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 '660237%' UNION ALL SELECT '通讯费' AS subject_name -- 科目名称 ,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 '660287%' UNION ALL SELECT '会议费' AS subject_name -- 科目名称 ,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 '660228%' UNION ALL SELECT '其他' AS subject_name -- 科目名称 ,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 NOT LIKE '660297%' AND subject_code NOT LIKE '660219%' AND subject_code NOT LIKE '660289%' AND subject_code NOT LIKE '660243%' AND subject_code NOT LIKE '660226%' AND subject_code NOT LIKE '660234%' AND subject_code NOT LIKE '660221%' AND subject_code NOT LIKE '660227%' AND subject_code NOT LIKE '660232%' AND subject_code NOT LIKE '660233%' AND subject_code NOT LIKE '660231%' AND subject_code NOT LIKE '660273%' AND subject_code NOT LIKE '660290%' AND subject_code NOT LIKE '660237%' AND subject_code NOT LIKE '660287%' AND subject_code NOT LIKE '660228%' ) ) SELECT subject_name AS subject_name -- 科目名称 ,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 ORDER BY DECODE(subject_name,'职工薪酬','a','折旧费','b','物料消耗','c','存货损耗','d','办公费','e','咨询服务费','f','无形资产摊销','g','差旅费','h','物业管理费','i','水电气费','j','车辆使用费','k','业务招待费','l','使用权资产摊销','m','租赁费','n','通讯费','o','会议费','p','其他','q')