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 '6602%' 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 '6602%' 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 -- 科目名称 ,'660297' 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 '660297%' UNION ALL SELECT '折旧费' AS subject_name -- 科目名称 ,'660219' 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 '660219%' UNION ALL SELECT '物料消耗' AS subject_name -- 科目名称 ,'660289' 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 '660289%' UNION ALL SELECT '存货损耗' AS subject_name -- 科目名称 ,'660243' 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 '660243%' UNION ALL SELECT '办公费' AS subject_name -- 科目名称 ,'660226' 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 '660226%' UNION ALL SELECT '咨询服务费' AS subject_name -- 科目名称 ,'660234' 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 '660234%' UNION ALL SELECT '无形资产摊销' AS subject_name -- 科目名称 ,'660221' 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 '660221%' UNION ALL SELECT '差旅费' AS subject_name -- 科目名称 ,'660227' 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 '660227%' UNION ALL SELECT '物业管理费' AS subject_name -- 科目名称 ,'660232' 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 '660232%' UNION ALL SELECT '水电气费' AS subject_name -- 科目名称 ,'660233' 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 '660233%' UNION ALL SELECT '车辆使用费' AS subject_name -- 科目名称 ,'660231' 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 '660231%' UNION ALL SELECT '业务招待费' AS subject_name -- 科目名称 ,'660273' 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 '660273%' UNION ALL SELECT '使用权资产摊销' AS subject_name -- 科目名称 ,'660290' 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 '660290%' UNION ALL SELECT '租赁费' AS subject_name -- 科目名称 ,'660237' 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 '660237%' UNION ALL SELECT '通讯费' AS subject_name -- 科目名称 ,'660287' 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 '660287%' UNION ALL SELECT '会议费' AS subject_name -- 科目名称 ,'660228' 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 '660228%' 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 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 -- 科目名称 ,'660297' 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 '660297%' UNION ALL SELECT '折旧费' AS subject_name -- 科目名称 ,'660219' 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 '660219%' UNION ALL SELECT '物料消耗' AS subject_name -- 科目名称 ,'660289' 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 '660289%' UNION ALL SELECT '存货损耗' AS subject_name -- 科目名称 ,'660243' 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 '660243%' UNION ALL SELECT '办公费' AS subject_name -- 科目名称 ,'660226' 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 '660226%' UNION ALL SELECT '咨询服务费' AS subject_name -- 科目名称 ,'660234' 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 '660234%' UNION ALL SELECT '无形资产摊销' AS subject_name -- 科目名称 ,'660221' 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 '660221%' UNION ALL SELECT '差旅费' AS subject_name -- 科目名称 ,'660227' 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 '660227%' UNION ALL SELECT '物业管理费' AS subject_name -- 科目名称 ,'660232' 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 '660232%' UNION ALL SELECT '水电气费' AS subject_name -- 科目名称 ,'660233' 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 '660233%' UNION ALL SELECT '车辆使用费' AS subject_name -- 科目名称 ,'660231' 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 '660231%' UNION ALL SELECT '业务招待费' AS subject_name -- 科目名称 ,'660273' 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 '660273%' UNION ALL SELECT '使用权资产摊销' AS subject_name -- 科目名称 ,'660290' 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 '660290%' UNION ALL SELECT '租赁费' AS subject_name -- 科目名称 ,'660237' 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 '660237%' UNION ALL SELECT '通讯费' AS subject_name -- 科目名称 ,'660287' 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 '660287%' UNION ALL SELECT '会议费' AS subject_name -- 科目名称 ,'660228' 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 '660228%' 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 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%' ) ),tmp_data AS ( SELECT a.subject_name AS subject_name -- 科目名称 ,SUM(a.amt) AS amt FROM sdi_oth.ods_finance_budget_data a JOIN corp b on a.corp_name = b.corp_name WHERE fee_type = '管理费用' AND LEFT(years,4) = '${YEAR(sStart_mon)}' GROUP BY a.subject_name ) SELECT a.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 -- 借方金额_上年累计 ,IFNULL(SUM(b.amt),0) AS debit_amt_yusuan FROM ( SELECT subject_name AS subject_name -- 科目名称 , subject_code AS subject_code -- 科目编码 , SUM(debit_amt_m) AS debit_amt_m -- 借方金额_当月累计 , SUM(debit_amt_y) AS debit_amt_y -- 借方金额_当年累计 , SUM(debit_amt_last_m) AS debit_amt_last_m -- 借方金额_上年同期 , SUM(debit_amt_last_y) AS debit_amt_last_y -- 借方金额_上年累计 FROM subject_result GROUP BY subject_name , subject_code )a LEFT JOIN tmp_data b ON a.subject_name = b.subject_name GROUP BY a.subject_name,subject_code ORDER BY DECODE(a.subject_name,'职工薪酬','a','折旧费','b','物料消耗','c','存货损耗','d','办公费','e','咨询服务费','f','无形资产摊销','g','差旅费','h','物业管理费','i','水电气费','j','车辆使用费','k','业务招待费','l','使用权资产摊销','m','租赁费','n','通讯费','o','会议费','p','其他','q')