From 92c96bc84a7159cc42b833ec1dfd6d81761ddc11 Mon Sep 17 00:00:00 2001 From: yangkunan Date: Sat, 20 Sep 2025 13:09:04 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B5=84=E9=87=91=E7=AE=A1=E7=90=86=E9=A9=BE?= =?UTF-8?q?=E9=A9=B6=E8=88=B1=E4=BA=8C=E6=9C=9F+=E8=B4=A2=E5=8A=A1?= =?UTF-8?q?=E4=BA=8C=E6=9C=9F=E7=9B=B8=E5=85=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/sqldialects.xml | 3 + FineReport/财务主题/填报/费用/科目配置表.sql | 49 + FineReport/财务主题/填报/费用/预算填报.sql | 59 + .../财务主题/报表/负债/采购应付款明细表.sql | 3 + FineReport/财务主题/看板/资金管理驾驶舱.sql | 1562 ++++++++++++++++- 5 files changed, 1675 insertions(+), 1 deletion(-) create mode 100644 FineReport/财务主题/填报/费用/科目配置表.sql create mode 100644 FineReport/财务主题/填报/费用/预算填报.sql create mode 100644 FineReport/财务主题/报表/负债/采购应付款明细表.sql diff --git a/.idea/sqldialects.xml b/.idea/sqldialects.xml index 60d698f..179a4c3 100644 --- a/.idea/sqldialects.xml +++ b/.idea/sqldialects.xml @@ -12,7 +12,10 @@ + + + diff --git a/FineReport/财务主题/填报/费用/科目配置表.sql b/FineReport/财务主题/填报/费用/科目配置表.sql new file mode 100644 index 0000000..470d6fe --- /dev/null +++ b/FineReport/财务主题/填报/费用/科目配置表.sql @@ -0,0 +1,49 @@ +-- ***************************** +-- 创建人员: 杨坤安 +-- 创建时间: 2025年7月2日 +-- 功能描述: 科目配置表 +-- ***************************** + + +DROP TABLE IF EXISTS sdi_xdata.fr_fim_fee_subject_config; + +CREATE TABLE sdi_xdata.fr_fim_fee_subject_config ( + id varchar(200) PRIMARY KEY NOT NULL + ,subject_code varchar(100) + ,subject_name varchar(200) + ,fee_type varchar(100) + ,sorted_no integer + ,create_user varchar(100) + ,create_date timestamp + ,update_by varchar(50) + ,update_date timestamp +); + +-- 为表添加注释 +COMMENT ON TABLE sdi_xdata.fr_fim_fee_subject_config IS '财务费用科目配置表'; + +-- 为字段添加注释 +COMMENT ON COLUMN sdi_xdata.fr_fim_fee_subject_config.id IS '主键ID'; +COMMENT ON COLUMN sdi_xdata.fr_fim_fee_subject_config.subject_code IS '科目编码'; +COMMENT ON COLUMN sdi_xdata.fr_fim_fee_subject_config.subject_name IS '科目名称'; +COMMENT ON COLUMN sdi_xdata.fr_fim_fee_subject_config.fee_type IS '费用类型'; +COMMENT ON COLUMN sdi_xdata.fr_fim_fee_subject_config.sorted_no IS '排序字段'; +COMMENT ON COLUMN sdi_xdata.fr_fim_fee_subject_config.create_user IS '创建人'; +COMMENT ON COLUMN sdi_xdata.fr_fim_fee_subject_config.create_date IS '创建日期'; +COMMENT ON COLUMN sdi_xdata.fr_fim_fee_subject_config.update_by IS '更新人'; +COMMENT ON COLUMN sdi_xdata.fr_fim_fee_subject_config.update_date IS '更新日期'; + +SELECT +id -- 主键ID +,fee_type -- 费用类型 +,subject_code -- 科目编码 +,subject_name -- 科目名称 +,sorted_no +,create_user -- 创建人 +,create_date -- 创建日期 +,update_by -- 更新人 +,update_date -- 更新日期 +FROM sdi_xdata.fr_fim_fee_subject_config +WHERE 1 = 1 +${if(len(sStart_mon)=0,"","AND year_month = '"+sStart_mon+"'")} +${if(len(sFee_type)=0,"","AND fee_type = '"+sFee_type+"'")} \ No newline at end of file diff --git a/FineReport/财务主题/填报/费用/预算填报.sql b/FineReport/财务主题/填报/费用/预算填报.sql new file mode 100644 index 0000000..6ed990a --- /dev/null +++ b/FineReport/财务主题/填报/费用/预算填报.sql @@ -0,0 +1,59 @@ +-- ***************************** +-- 创建人员: 杨坤安 +-- 创建时间: 2025年6月23日 +-- 功能描述: 费用预算数据填报 +-- ***************************** + + +DROP TABLE IF EXISTS sdi_xdata.fr_fim_fee_budget; +DROP TABLE IF EXISTS sdi_xdata.fim_fee_budget; + +CREATE TABLE sdi_xdata.fim_fee_budget ( + id varchar(200) PRIMARY KEY NOT NULL + ,years varchar(50) + ,fee_type varchar(50) + ,subject_code varchar(100) + ,subject_name varchar(200) + ,corp_code varchar(100) + ,corp_name varchar(200) + ,budget_fee decimal(32,16) + ,create_user varchar(100) + ,create_date timestamp + ,update_by varchar(50) + ,update_date timestamp +); + +-- 为表添加注释 +COMMENT ON TABLE sdi_xdata.fim_fee_budget IS '财务预算费用数据维护'; + +-- 为字段添加注释 +COMMENT ON COLUMN sdi_xdata.fim_fee_budget.id IS '主键ID'; +COMMENT ON COLUMN sdi_xdata.fim_fee_budget.years IS '预算年'; +COMMENT ON COLUMN sdi_xdata.fim_fee_budget.fee_type IS '费用类型'; +COMMENT ON COLUMN sdi_xdata.fim_fee_budget.subject_code IS '科目编码'; +COMMENT ON COLUMN sdi_xdata.fim_fee_budget.subject_name IS '科目名称'; +COMMENT ON COLUMN sdi_xdata.fim_fee_budget.corp_code IS '公司编码'; +COMMENT ON COLUMN sdi_xdata.fim_fee_budget.corp_name IS '公司名称'; +COMMENT ON COLUMN sdi_xdata.fim_fee_budget.budget_fee IS '预算费用'; +COMMENT ON COLUMN sdi_xdata.fim_fee_budget.create_user IS '创建人'; +COMMENT ON COLUMN sdi_xdata.fim_fee_budget.create_date IS '创建日期'; +COMMENT ON COLUMN sdi_xdata.fim_fee_budget.update_by IS '更新人'; +COMMENT ON COLUMN sdi_xdata.fim_fee_budget.update_date IS '更新日期'; + +SELECT +id -- 主键ID +,years -- 年月 +,fee_type -- 费用类型 +,subject_code -- 科目编码 +,subject_name -- 科目名称 +,corp_code -- 公司编码 +,corp_name -- 公司名称 +,budget_fee -- 预算费用 +,create_user -- 创建人 +,create_date -- 创建日期 +,update_by -- 更新人 +,update_date -- 更新日期 +FROM sdi_xdata.fim_fee_budget +WHERE 1 = 1 +${if(len(sStart_mon)=0,"","AND year_month = '"+sStart_mon+"'")} +${if(len(sFee_type)=0,"","AND fee_type = '"+sFee_type+"'")} \ No newline at end of file diff --git a/FineReport/财务主题/报表/负债/采购应付款明细表.sql b/FineReport/财务主题/报表/负债/采购应付款明细表.sql new file mode 100644 index 0000000..8e1be61 --- /dev/null +++ b/FineReport/财务主题/报表/负债/采购应付款明细表.sql @@ -0,0 +1,3 @@ +SELECT + * +FROM dwi.dwi_fim_pur_account \ No newline at end of file diff --git a/FineReport/财务主题/看板/资金管理驾驶舱.sql b/FineReport/财务主题/看板/资金管理驾驶舱.sql index ae46d2c..52e5a4b 100644 --- a/FineReport/财务主题/看板/资金管理驾驶舱.sql +++ b/FineReport/财务主题/看板/资金管理驾驶舱.sql @@ -1448,4 +1448,1564 @@ where current_balance!=0 is_all = 1 ) ) -group by re.corp_code,if((dba.category_name in ('专用户','保证金户') and re.bank_account!='22195901040000147' ) or re.bank_account='信用证保证金',1,0) \ No newline at end of file +group by re.corp_code,if((dba.category_name in ('专用户','保证金户') and re.bank_account!='22195901040000147' ) or re.bank_account='信用证保证金',1,0) + + + +-- ***************************** +-- 创建人员: 杨坤安 +-- 创建时间: 2025-09-16 14:35 +-- 功能描述: 资金看板二期内容 +-- ***************************** + +-- ------------------------------- +-- 日报_银行负债余额 +-- ------------------------------- + +-- 银行 +-- 贷款余额 +-- 票据余额 +-- 合计 +-- 较年初增长 年初合计 +-- 同期增长额 去年同期合计 + +with data_dk as( + select + bank_code + ,corp_code as org_code + ,occupy_credit_quota_type + ,sum(case when to_char(case when actual_amort_day is not null then actual_amort_day else term_day end::date,'yyyy-MM-dd') > sysdate and to_char(loan_day::date,'yyyy-MM-dd') <= sysdate then term_amt else 0 end) this_amt -- 贷款金额 + ,sum(case when to_char(case when actual_amort_day is not null then actual_amort_day else term_day end::date,'yyyy-MM-dd') > (DATE_TRUNC('year', sysdate) - INTERVAL '1 day')::DATE and to_char(loan_day::date,'yyyy-MM-dd') <= (DATE_TRUNC('year', sysdate) - INTERVAL '1 day')::DATE then term_amt else 0 end) year_amt -- 年初合计 + ,sum(case when to_char(loan_day::date,'yyyy-MM-dd') <= sysdate::date - interval '1 year' then term_amt else 0 end) last_amt -- 去年同期合计 + from + sdi_xdata.fim_liab_amort_plan A + where -- 优先获取实际还款日期,再取到期日期 + to_char(case when actual_amort_day is not null then actual_amort_day else term_day end::date,'yyyy-MM-dd') > sysdate::date - interval '1 year' -- 去年同期的日期 + and to_char(loan_day::date,'yyyy-MM-dd') <= sysdate::date -- 借款日期要在查询日期之前的 + group by + bank_code, + corp_code , + occupy_credit_quota_type +), + data_yf as ( + select + a.draw_bank_code --出票人银行 + ,a.corp_code + ,'应付票据金额' as occupy_credit_quota_type + ,sum(case when to_char(end_date,'yyyy-MM-dd') > sysdate::date and to_char(issue_date,'yyyy-MM-dd') <= sysdate::date then a.amt else 0 end) this_amt -- 票面金额 + ,sum(case when to_char(end_date,'yyyy-MM-dd') > (DATE_TRUNC('year', sysdate) - INTERVAL '1 day')::DATE and to_char(issue_date,'yyyy-MM-dd') <= (DATE_TRUNC('year', sysdate) - INTERVAL '1 day')::DATE then a.amt else 0 end) year_amt -- 年初合计 + ,sum(case when to_char(issue_date,'yyyy-MM-dd') <= sysdate::date - interval '1 year' then a.amt else 0 end) last_amt -- 去年同期 + from + dwr.fact_fim_draft_flow a + where + operate_flag='1003' -- 开票 + and to_char(end_date,'yyyy-MM-dd') > sysdate::date - interval '1 year' -- 到期日期 + and to_char(issue_date,'yyyy-MM-dd') <= sysdate::date -- 出票日期 + group by + a.corp_code,a.draw_bank_code + order by + a.corp_code,a.draw_bank_code + ), + dz as ( + SELECT distinct + bank.code AS bank_code, + bank.abbreviation AS bank_name, + org.code AS org_code, + org.abbreviation AS org_name + FROM + sdi_xdata.fim_liab_compare bank, + sdi_xdata.fim_liab_compare org + WHERE + bank.LABEL = '银行' + AND org.LABEL = '主体' + ) +SELECT + dz.bank_code as bank_code + ,dz.bank_name as bank + -- ,dk.occupy_credit_quota_type + ,SUM(CASE WHEN dk.occupy_credit_quota_type <> '应付票据金额' THEN dk.this_amt ELSE 0 END) * (1/10000) AS loan_amt -- 贷款余额 + ,SUM(CASE WHEN dk.occupy_credit_quota_type = '应付票据金额' THEN dk.this_amt ELSE 0 END) * (1/10000) AS payable_amt -- 应付票据金额 + ,SUM(dk.this_amt) * (1/10000) as this_amt -- 合计 + ,(SUM(dk.this_amt) - SUM(dk.year_amt)) * (1/10000) AS year_add -- 较年初增长 + ,(SUM(dk.this_amt) - SUM(dk.last_amt)) * (1/10000) AS last_year_add +FROM + dz + left JOIN (select * from data_dk union all select * from data_yf ) AS dk ON dk.org_code = dz.org_code and dz.bank_code = dk.bank_code +where + dk.this_amt is not null or dk.year_amt is not null or dk.last_amt is not null +group by dz.bank_code,dz.bank_name +; + +-- ====================================== +-- 资金收支存总览 +-- ====================================== +-- 总收入、总支出 + +with rlt as ( + SELECT decode(trade_direction,'票据变现收入','支出',trade_direction) as trade_direction, + (CASE WHEN trade_direction = '收入' + then + (case when plan_item_name = '对外销售酒类回款' AND corp_code <> '0201' + then '成品酒销售回款' + when plan_item_name = '对外销售酒类回款' AND corp_code = '0201' + then '散酒销售回款' + when plan_item_name in ('保证金','共建基金') or (plan_item_name='其他收入(含未确认收入)' and trade_item IN ('红包充值费用','转货款')) + then '红包款/转货款/保证金/共建' + when plan_item_name IN('取得贷款','开票') + then '银行融资' + when plan_item_name IN('搬迁补偿/政府补助','政府返还及补助') + then '政府返还及补助' + when plan_item_name='票据变现收入' + then '其他(捐赠等)' + when plan_item_name IN('对外销售非酒类回款','吸收投资','税费返还','其他收入(含未确认收入)','利息收入') and nvl(trade_item,'x') not IN ('红包充值费用','转货款') + then '其他零星收款' end) + WHEN trade_direction = '支出' + then (case when plan_item_name in('原材料','辅料','低值易耗') + then '原辅低耗材料' + when plan_item_name in('散酒') + then '外购散酒' + when plan_item_name in('外购包材','外协加工费') + then '外购包材/外协加工' + when plan_item_name in('其它制造费用','水电气费') + then '制造费用/能源费用' + when plan_item_name in('广告费','市场活动投入','其它销售费用','委外服务(体验体系)','酒店采购','储运费') + then '广告/市场活动/庄园采购/储运' + when plan_item_name in('退共建基金') + then '共建支出' + when plan_item_name in('退经销商货款') + then '经销商退款' + when plan_item_name in('职工薪酬及福利') + then '薪酬及福利' + when plan_item_name in('税费') + then '税金缴纳' + when plan_item_name in('其它管理费用','财务手续费用','研发费用') + then '管理费用/研发费用' + when plan_item_name in ('土地支出','土建支出','利息支出','分配股利') + then plan_item_name + when plan_item_name in('设备支出','维修支出') + then '设备及维修支出' + when plan_item_name in('偿还贷款','开票兑付') + then '归还贷款' + else '其他(捐赠等)' end ) + else plan_item_name + end ) as fud_project_item, + + case when plan_item_name in ('票据变现收入','票据变现支出') + then '票据贴现费用' + when plan_item_name='其他收入(含未确认收入)' + then if(nvl(trade_item,'x') IN ('红包充值费用','转货款','窜货款'),trade_item,'其他') + else plan_item_name end as plan_item_name, + if(plan_item_name='其他收入(含未确认收入)' ,decode(trade_item,'转货款',12.1,'红包充值费用',12.2,'窜货款',12.3,st.sorted_no),nvl(st.sorted_no,99)) as srt, + SUM(CASE WHEN stat_date = '${sStart_date}' -- 当前统计时间 + THEN amt ELSE 0 END) / 10000 AS today_amt -- 本日 + ,SUM(CASE WHEN to_char(stat_date,'yyyy-MM') = '${sStart_mon}' -- 当前统计时间 + THEN amt ELSE 0 END) / 10000 AS month_amt -- 本月累计 + ,SUM(CASE WHEN to_char(stat_date,'yyyy') = '${sStart_year}' + THEN amt ELSE 0 END) / 10000 AS year_amt -- 本年累计 + ,SUM(CASE WHEN to_char(stat_date,'yyyy') = '${YEAR(YEARDELTA(CONCATENATE(sStart_year,"-01-01"),-1))}' + THEN amt ELSE 0 END) / 10000 AS last_year_amt -- 去年同期累计 + FROM dm.dm_fim_fud_indicator fp + left join sdi_xdata.mdm_dim_sorted st on fp.plan_item_name=st.sorted_value and st.sorted_type='资金项目' + where fp.trade_direction IN('收入','支出') + and fp.indi_type='执行' +-- and NVL(fp.owner_bank_account,'1') <> '22195101040012008' + AND fp.plan_item_name NOT IN ('内部调拨支出','内部调拨收入') + and EXISTS ( + SELECT + 1 + FROM + dm.dm_bi_user_permisson + WHERE + bi_user = '${fine_username}' + AND per_model IN ('all','fim') + AND ( + is_all = 1 + OR per_code = fp.corp_code + ) + ${IF(LEN(sCorp)!=0," AND fp.corp_code IN ('"+ sCorp +"')", "")} + ) + + AND stat_date >= date '${DATEINYEAR(YEARDELTA(sStart_date,-1),1)}' -- 上年年初 + -- and stat_date<= date '${sStart_date}' -- 当前统计时间 + group by + (CASE WHEN trade_direction = '收入' + then + (case when plan_item_name = '对外销售酒类回款' AND corp_code <> '0201' + then '成品酒销售回款' + when plan_item_name = '对外销售酒类回款' AND corp_code = '0201' + then '散酒销售回款' + when plan_item_name in ('保证金','共建基金') or (plan_item_name='其他收入(含未确认收入)' and trade_item IN ('红包充值费用','转货款')) + then '红包款/转货款/保证金/共建' + when plan_item_name IN('取得贷款','开票') + then '银行融资' + when plan_item_name IN('搬迁补偿/政府补助','政府返还及补助') + then '政府返还及补助' + when plan_item_name='票据变现收入' + then '其他(捐赠等)' + when plan_item_name IN('对外销售非酒类回款','吸收投资','税费返还','其他收入(含未确认收入)','利息收入') and nvl(trade_item,'x') not IN ('红包充值费用','转货款') + then '其他零星收款' end) + WHEN trade_direction = '支出' + then (case when plan_item_name in('原材料','辅料','低值易耗') + then '原辅低耗材料' + when plan_item_name in('散酒') + then '外购散酒' + when plan_item_name in('外购包材','外协加工费') + then '外购包材/外协加工' + when plan_item_name in('其它制造费用','水电气费') + then '制造费用/能源费用' + when plan_item_name in('广告费','市场活动投入','其它销售费用','委外服务(体验体系)','酒店采购','储运费') + then '广告/市场活动/庄园采购/储运' + when plan_item_name in('退共建基金') + then '共建支出' + when plan_item_name in('退经销商货款') + then '经销商退款' + when plan_item_name in('职工薪酬及福利') + then '薪酬及福利' + when plan_item_name in('税费') + then '税金缴纳' + when plan_item_name in('其它管理费用','财务手续费用','研发费用') + then '管理费用/研发费用' + when plan_item_name in ('土地支出','土建支出','利息支出','分配股利') + then plan_item_name + when plan_item_name in('设备支出','维修支出') + then '设备及维修支出' + when plan_item_name in('偿还贷款','开票兑付') + then '归还贷款' + else '其他(捐赠等)' end ) + else plan_item_name + end ), + case when plan_item_name in ('票据变现收入','票据变现支出') + then '票据贴现费用' + when plan_item_name='其他收入(含未确认收入)' + then if(nvl(trade_item,'x') IN ('红包充值费用','转货款','窜货款'),trade_item,'其他') + else plan_item_name end, + if(plan_item_name='其他收入(含未确认收入)' ,decode(trade_item,'转货款',12.1,'红包充值费用',12.2,'窜货款',12.3,st.sorted_no),nvl(st.sorted_no,99)), + decode(trade_direction,'票据变现收入','支出',trade_direction) + union all + SELECT + '收入' as trade_direction, + '其他零星收款' as fud_project_item, + '汇兑损益' as plan_item_name, + 99 as srt, + SUM(CASE WHEN last_day(date(concat_ws(',',account_year,account_month,'1'))) = '${sStart_date}' -- 当前统计时间 + THEN losses_amt ELSE 0 END) / 10000 AS today_amt -- 本日 + ,SUM(CASE WHEN to_char(date(concat_ws(',',account_year,account_month,'1')),'yyyy-MM') = '${sStart_mon}' -- 当前统计时间 + THEN losses_amt ELSE 0 END) / 10000 AS month_amt -- 本月累计 + ,SUM(CASE WHEN to_char(date(concat_ws(',',account_year,account_month,'1')),'yyyy') = '${sStart_year}' -- 当前统计时间 + THEN losses_amt ELSE 0 END) / 10000 AS year_amt -- 本年累计 + ,SUM(CASE WHEN to_char(date(concat_ws(',',account_year,account_month,'1')),'yyyy') = '${YEAR(YEARDELTA(CONCATENATE(sStart_year,"-01-01"),-1))}' + THEN losses_amt ELSE 0 END) / 10000 AS last_year_amt -- 去年同期累计 + FROM sdi_hzg.exchange_gains_losses fp + where + EXISTS ( + SELECT + 1 + FROM + dm.dm_bi_user_permisson + WHERE + bi_user = '${fine_username}' + AND per_model IN ('all','fim') + AND ( + is_all = 1 + OR per_code = fp.corp_code + ) + ) + ${IF(LEN(sCorp)!=0," AND fp.corp_code IN ('"+ sCorp +"')", "")} + + AND date(concat_ws(',',account_year,account_month,'1')) >= date '${DATEINYEAR(YEARDELTA(sStart_date,-1),1)}' -- 上年年初 + -- and date(concat_ws(',',account_year,account_month,'1'))<= date '${sStart_date}' -- 当前统计时间 +) +SELECT +SUM(CASE WHEN trade_direction = '收入' THEN ${switch(sData_type,'0',"today_amt",'1',"month_amt",'2',"year_amt")} ELSE 0 END) AS in_amt +,SUM(CASE WHEN trade_direction = '支出' THEN ${switch(sData_type,'0',"today_amt",'1',"month_amt",'2',"year_amt")} ELSE 0 END) AS out_amt + +FROM rlt +; +-- 资金余额 + +with dba as ( + select decode(length(accountnumber), '17', + substring(accountnumber, 3), + accountnumber) as accountnumber, + max(category_name) as category_name + + from dwr.dim_bank_account + where account_owner_class='langjiu' + group by decode(length(accountnumber), '17', + substring(accountnumber, 3), + accountnumber) +), + lmt as (/*用于计算受限资金*/ + select + report_date, + corp_code, + bank_account, + currency, + /*20351059900100000512291 为受限资金账户*/ + current_balance-decode(bank_account,'20351059900100000512291',0,usable_balance)-nvl(lag(current_balance-decode(bank_account,'20351059900100000512291',0,usable_balance),1) over (partition by bank_account,currency order by report_date),0) as limit_amt_pre + from dwi.dwi_fim_bank_account_balance + ), ye as ( + + select corp_code, + owner_bank_account, + amt, + amt_type, + decode(is_usable,1,0,1) as is_usable + from dm.dm_fim_fud_balance + where stat_date=date'${DATEINMONTH(MONTHDELTA(sStart_date,-1),1)}' + union all + /*汇兑损益*/ + SELECT + corp_code, + null as owner_bank_account, + losses_amt as amt, + '现金' as amt_type, + 0 as is_usable + FROM sdi_hzg.exchange_gains_losses + where date(concat_ws(',',account_year,account_month,'1'))=date'${DATEINMONTH(sStart_date,1)}' + union all + select + corp_code, + owner_bank_account, + sum(decode(trade_direction,'支出',-1*amt,amt)) as amt, + amt_type, + 0 as is_usable + from dm.dm_fim_fud_indicator + where indi_type='执行' + and stat_date>=date'${DATEINMONTH(sStart_date,1)}' + and stat_date<=date'${sStart_date}' + --and plan_item_name not in ('内部调拨收入', '内部调拨支出') + group by corp_code, + owner_bank_account, + amt_type + union all +/*受限资金*/ + select corp_code, + bank_account as owner_bank_account, + sum(limit_amt_pre) as amt, + '现金' as amt_type, + 1 as is_usable + from lmt + where limit_amt_pre!=0 + and report_date>=date'${DATEINMONTH(sStart_date,1)}' + and report_date<=date'${sStart_date}' + group by corp_code,bank_account + union all +/*受限资金*/ + select corp_code, + bank_account as owner_bank_account, + -1*sum(limit_amt_pre) as amt, + '现金' as amt_type, + 0 as is_usable + from lmt + where limit_amt_pre!=0 + and report_date>=date'${DATEINMONTH(sStart_date,1)}' + and report_date<=date'${sStart_date}' + group by corp_code,bank_account +) +select + -- a.corp_code, + sum(a.amt) * (1/10000) as yb_balance + +-- is_usable as is_limit--受限资金金额 +from ye a + inner join dwr.dim_corp c on a.corp_code=c.corp_code and c.corp_group='股份' +/*left join dba on decode(length(a.owner_bank_account), 17, + substring(a.owner_bank_account, 3), + a.owner_bank_account)=dba.accountnumber*/ +where exists ( + select + 1 + from + dm.dm_bi_user_permisson + where + bi_user = '${fine_username}' + and per_model IN ('all','fim') + and ( + is_all = 1 + ) +) + and a.amt!=0 + ${IF(LEN(sCorp)!=0," AND a.corp_code IN ('"+ sCorp +"')", "")} +; + +-- ====================================== +-- 资金分析 +-- ====================================== + +-- 收入支出指标卡 + + +with rlt as ( + SELECT decode(trade_direction,'票据变现收入','支出',trade_direction) as trade_direction, + (CASE WHEN trade_direction = '收入' + then + (case when plan_item_name = '对外销售酒类回款' AND corp_code <> '0201' + then '成品酒销售回款' + when plan_item_name = '对外销售酒类回款' AND corp_code = '0201' + then '散酒销售回款' + when plan_item_name in ('保证金','共建基金') or (plan_item_name='其他收入(含未确认收入)' and trade_item IN ('红包充值费用','转货款')) + then '红包款/转货款/保证金/共建' + when plan_item_name IN('取得贷款','开票') + then '银行融资' + when plan_item_name IN('搬迁补偿/政府补助','政府返还及补助') + then '政府返还及补助' + when plan_item_name='票据变现收入' + then '其他(捐赠等)' + when plan_item_name IN('对外销售非酒类回款','吸收投资','税费返还','其他收入(含未确认收入)','利息收入') and nvl(trade_item,'x') not IN ('红包充值费用','转货款') + then '其他零星收款' end) + WHEN trade_direction = '支出' + then (case when plan_item_name in('原材料','辅料','低值易耗') + then '原辅低耗材料' + when plan_item_name in('散酒') + then '外购散酒' + when plan_item_name in('外购包材','外协加工费') + then '外购包材/外协加工' + when plan_item_name in('其它制造费用','水电气费') + then '制造费用/能源费用' + when plan_item_name in('广告费','市场活动投入','其它销售费用','委外服务(体验体系)','酒店采购','储运费') + then '广告/市场活动/庄园采购/储运' + when plan_item_name in('退共建基金') + then '共建支出' + when plan_item_name in('退经销商货款') + then '经销商退款' + when plan_item_name in('职工薪酬及福利') + then '薪酬及福利' + when plan_item_name in('税费') + then '税金缴纳' + when plan_item_name in('其它管理费用','财务手续费用','研发费用') + then '管理费用/研发费用' + when plan_item_name in ('土地支出','土建支出','利息支出','分配股利') + then plan_item_name + when plan_item_name in('设备支出','维修支出') + then '设备及维修支出' + when plan_item_name in('偿还贷款','开票兑付') + then '归还贷款' + else '其他(捐赠等)' end ) + else plan_item_name + end ) as fud_project_item, + + case when plan_item_name in ('票据变现收入','票据变现支出') + then '票据贴现费用' + when plan_item_name='其他收入(含未确认收入)' + then if(nvl(trade_item,'x') IN ('红包充值费用','转货款','窜货款'),trade_item,'其他') + else plan_item_name end as plan_item_name, + if(plan_item_name='其他收入(含未确认收入)' ,decode(trade_item,'转货款',12.1,'红包充值费用',12.2,'窜货款',12.3,st.sorted_no),nvl(st.sorted_no,99)) as srt, + SUM(CASE WHEN stat_date = '${sStart_date}' -- 当前统计时间 + THEN amt ELSE 0 END) / 10000 AS today_amt -- 本日 + ,SUM(CASE WHEN to_char(stat_date,'yyyy-MM') = '${sStart_mon}' -- 当前统计时间 + THEN amt ELSE 0 END) / 10000 AS month_amt -- 本月累计 + ,SUM(CASE WHEN to_char(stat_date,'yyyy') = '${sStart_year}' + THEN amt ELSE 0 END) / 10000 AS year_amt -- 本年累计 + + ,SUM(CASE WHEN stat_date = '${YEARDELTA(sStart_date,-1)}' -- 当前统计时间 + THEN amt ELSE 0 END) / 10000 AS last_year_day_amt -- 去年同期本日 + ,SUM(CASE WHEN to_char(stat_date,'yyyy-MM') = '${LEFT(YEARDELTA(CONCATENATE(sStart_mon,"-01"),-1),7)}' + THEN amt ELSE 0 END) / 10000 AS last_year_mon_amt -- 去年同期本月累计 + ,SUM(CASE WHEN to_char(stat_date,'yyyy') = '${YEAR(YEARDELTA(CONCATENATE(sStart_year,"-01-01"),-1))}' + THEN amt ELSE 0 END) / 10000 AS last_year_amt -- 去年同期累计 + FROM dm.dm_fim_fud_indicator fp + left join sdi_xdata.mdm_dim_sorted st on fp.plan_item_name=st.sorted_value and st.sorted_type='资金项目' + where fp.trade_direction IN('收入','支出') + and fp.indi_type='执行' +-- and NVL(fp.owner_bank_account,'1') <> '22195101040012008' + AND fp.plan_item_name NOT IN ('内部调拨支出','内部调拨收入') + and EXISTS ( + SELECT + 1 + FROM + dm.dm_bi_user_permisson + WHERE + bi_user = '${fine_username}' + AND per_model IN ('all','fim') + AND ( + is_all = 1 + OR per_code = fp.corp_code + ) + ${IF(LEN(sCorp)!=0," AND fp.corp_code IN ('"+ sCorp +"')", "")} + ) + + AND stat_date >= date '${DATEINYEAR(YEARDELTA(sStart_date,-1),1)}' -- 上年年初 + -- and stat_date<= date '${sStart_date}' -- 当前统计时间 + group by + (CASE WHEN trade_direction = '收入' + then + (case when plan_item_name = '对外销售酒类回款' AND corp_code <> '0201' + then '成品酒销售回款' + when plan_item_name = '对外销售酒类回款' AND corp_code = '0201' + then '散酒销售回款' + when plan_item_name in ('保证金','共建基金') or (plan_item_name='其他收入(含未确认收入)' and trade_item IN ('红包充值费用','转货款')) + then '红包款/转货款/保证金/共建' + when plan_item_name IN('取得贷款','开票') + then '银行融资' + when plan_item_name IN('搬迁补偿/政府补助','政府返还及补助') + then '政府返还及补助' + when plan_item_name='票据变现收入' + then '其他(捐赠等)' + when plan_item_name IN('对外销售非酒类回款','吸收投资','税费返还','其他收入(含未确认收入)','利息收入') and nvl(trade_item,'x') not IN ('红包充值费用','转货款') + then '其他零星收款' end) + WHEN trade_direction = '支出' + then (case when plan_item_name in('原材料','辅料','低值易耗') + then '原辅低耗材料' + when plan_item_name in('散酒') + then '外购散酒' + when plan_item_name in('外购包材','外协加工费') + then '外购包材/外协加工' + when plan_item_name in('其它制造费用','水电气费') + then '制造费用/能源费用' + when plan_item_name in('广告费','市场活动投入','其它销售费用','委外服务(体验体系)','酒店采购','储运费') + then '广告/市场活动/庄园采购/储运' + when plan_item_name in('退共建基金') + then '共建支出' + when plan_item_name in('退经销商货款') + then '经销商退款' + when plan_item_name in('职工薪酬及福利') + then '薪酬及福利' + when plan_item_name in('税费') + then '税金缴纳' + when plan_item_name in('其它管理费用','财务手续费用','研发费用') + then '管理费用/研发费用' + when plan_item_name in ('土地支出','土建支出','利息支出','分配股利') + then plan_item_name + when plan_item_name in('设备支出','维修支出') + then '设备及维修支出' + when plan_item_name in('偿还贷款','开票兑付') + then '归还贷款' + else '其他(捐赠等)' end ) + else plan_item_name + end ), + case when plan_item_name in ('票据变现收入','票据变现支出') + then '票据贴现费用' + when plan_item_name='其他收入(含未确认收入)' + then if(nvl(trade_item,'x') IN ('红包充值费用','转货款','窜货款'),trade_item,'其他') + else plan_item_name end, + if(plan_item_name='其他收入(含未确认收入)' ,decode(trade_item,'转货款',12.1,'红包充值费用',12.2,'窜货款',12.3,st.sorted_no),nvl(st.sorted_no,99)), + decode(trade_direction,'票据变现收入','支出',trade_direction) + union all + SELECT + '收入' as trade_direction, + '其他零星收款' as fud_project_item, + '汇兑损益' as plan_item_name, + 99 as srt, + SUM(CASE WHEN last_day(date(concat_ws(',',account_year,account_month,'1'))) = '${sStart_date}' -- 当前统计时间 + THEN losses_amt ELSE 0 END) / 10000 AS today_amt -- 本日 + ,SUM(CASE WHEN to_char(date(concat_ws(',',account_year,account_month,'1')),'yyyy-MM') = '${sStart_mon}' -- 当前统计时间 + THEN losses_amt ELSE 0 END) / 10000 AS month_amt -- 本月累计 + ,SUM(CASE WHEN to_char(date(concat_ws(',',account_year,account_month,'1')),'yyyy') = '${sStart_year}' -- 当前统计时间 + THEN losses_amt ELSE 0 END) / 10000 AS year_amt -- 本年累计 + + ,SUM(CASE WHEN last_day(date(concat_ws(',',account_year,account_month,'1'))) = '${YEARDELTA(sStart_date,-1)}' + THEN losses_amt ELSE 0 END) / 10000 AS last_year_day_amt -- 去年同期本日累计 + ,SUM(CASE WHEN to_char(date(concat_ws(',',account_year,account_month,'1')),'yyyy-MM') = '${LEFT(YEARDELTA(CONCATENATE(sStart_mon,"-01"),-1),7)}' + THEN losses_amt ELSE 0 END) / 10000 AS last_year_mon_amt -- 去年同期本月累计 + ,SUM(CASE WHEN to_char(date(concat_ws(',',account_year,account_month,'1')),'yyyy') = '${YEAR(YEARDELTA(CONCATENATE(sStart_year,"-01-01"),-1))}' + -- and date(concat_ws(',',account_year,account_month,'1'))<=date'${YEARDELTA(sStart_date,-1)}' + THEN losses_amt ELSE 0 END) / 10000 AS last_year_amt -- 去年同期累计 + FROM sdi_hzg.exchange_gains_losses fp + where + EXISTS ( + SELECT + 1 + FROM + dm.dm_bi_user_permisson + WHERE + bi_user = '${fine_username}' + AND per_model IN ('all','fim') + AND ( + is_all = 1 + OR per_code = fp.corp_code + ) + ) + ${IF(LEN(sCorp)!=0," AND fp.corp_code IN ('"+ sCorp +"')", "")} + + AND date(concat_ws(',',account_year,account_month,'1')) >= date '${DATEINYEAR(YEARDELTA(sStart_date,-1),1)}' -- 上年年初 + -- and date(concat_ws(',',account_year,account_month,'1'))<= date '${sStart_date}' -- 当前统计时间 +), +calc_base AS( + SELECT + -- 酒类回款 + SUM(CASE WHEN trade_direction = '收入' AND fud_project_item IN('成品酒销售回款','散酒销售回款') THEN ${switch(sData_type,'0',"today_amt",'1',"month_amt",'2',"year_amt")} ELSE 0 END) AS wine_in_amt -- 酒类回款本期收入 + ,SUM(CASE WHEN trade_direction = '收入' AND fud_project_item IN('成品酒销售回款','散酒销售回款') THEN ${switch(sData_type,'0',"last_year_day_amt",'1',"last_year_mon_amt",'2',"last_year_amt")} ELSE 0 END) AS wine_in_amt_yy -- 酒类回款同期收入 + -- 银行融资 + ,SUM(CASE WHEN trade_direction = '收入' AND fud_project_item IN('银行融资') THEN ${switch(sData_type,'0',"today_amt",'1',"month_amt",'2',"year_amt")} ELSE 0 END) AS bank_in_amt -- 银行融资本期收入 + ,SUM(CASE WHEN trade_direction = '收入' AND fud_project_item IN('银行融资') THEN ${switch(sData_type,'0',"last_year_day_amt",'1',"last_year_mon_amt",'2',"last_year_amt")} ELSE 0 END) AS bank_in_amt_yy -- 银行融资同期收入 + + -- 其他收入 + ,SUM(CASE WHEN trade_direction = '收入' AND fud_project_item NOT IN('银行融资','成品酒销售回款','散酒销售回款') THEN ${switch(sData_type,'0',"today_amt",'1',"month_amt",'2',"year_amt")} ELSE 0 END) AS other_in_amt -- 其它本期收入 + ,SUM(CASE WHEN trade_direction = '收入' AND fud_project_item NOT IN('银行融资','成品酒销售回款','散酒销售回款') THEN ${switch(sData_type,'0',"last_year_day_amt",'1',"last_year_mon_amt",'2',"last_year_amt")} ELSE 0 END) AS other_in_amt_yy -- 其他同期收入 + + -- 经营支出 + ,SUM(CASE WHEN trade_direction = '支出' AND fud_project_item NOT IN('归还贷款','土建支出','土地支出','设备及维修支出') THEN ${switch(sData_type,'0',"today_amt",'1',"month_amt",'2',"year_amt")} ELSE 0 END) AS oper_out_amt -- 经营本期支出 + ,SUM(CASE WHEN trade_direction = '支出' AND fud_project_item NOT IN('归还贷款','土建支出','土地支出','设备及维修支出') THEN ${switch(sData_type,'0',"last_year_day_amt",'1',"last_year_mon_amt",'2',"last_year_amt")} ELSE 0 END) AS oper_out_amt_yy -- 经营同期支出 + + -- 投资支出 + ,SUM(CASE WHEN trade_direction = '支出' AND fud_project_item IN('土建支出','土地支出','设备及维修支出') THEN ${switch(sData_type,'0',"today_amt",'1',"month_amt",'2',"year_amt")} ELSE 0 END) AS invest_out_amt -- 投资本期支出 + ,SUM(CASE WHEN trade_direction = '支出' AND fud_project_item IN('土建支出','土地支出','设备及维修支出') THEN ${switch(sData_type,'0',"last_year_day_amt",'1',"last_year_mon_amt",'2',"last_year_amt")} ELSE 0 END) AS invest_out_amt_yy -- 投资同期支出 + + -- 归还贷款 + ,SUM(CASE WHEN trade_direction = '支出' AND fud_project_item IN('归还贷款') THEN ${switch(sData_type,'0',"today_amt",'1',"month_amt",'2',"year_amt")} ELSE 0 END) AS repay_out_amt -- 归还贷款本期支出 + ,SUM(CASE WHEN trade_direction = '支出' AND fud_project_item IN('归还贷款') THEN ${switch(sData_type,'0',"last_year_day_amt",'1',"last_year_mon_amt",'2',"last_year_amt")} ELSE 0 END) AS repay_out_amt_yy -- 归还贷款同期支出 + FROM rlt +) +SELECT +-- 收入 + wine_in_amt -- 酒类回款 +,CASE WHEN wine_in_amt_yy = 0 THEN 0 + ELSE (wine_in_amt - wine_in_amt_yy) / wine_in_amt_yy END AS wine_in_amt_yy -- 酒类回款同比 +,bank_in_amt -- 银行融资 +,CASE WHEN bank_in_amt_yy = 0 THEN 0 + ELSE (bank_in_amt - bank_in_amt_yy) / bank_in_amt_yy END AS bank_in_amt_yy -- 银行融资同比 +,other_in_amt -- 其他 +,CASE WHEN other_in_amt_yy = 0 THEN 0 + ELSE (other_in_amt - other_in_amt_yy) / other_in_amt_yy END AS other_in_amt_yy -- -- 其他同比 +-- 支出 +,oper_out_amt -- 经营支出 +,CASE WHEN oper_out_amt_yy = 0 THEN 0 + ELSE (oper_out_amt - oper_out_amt_yy) / oper_out_amt_yy END AS oper_out_amt_yy -- 经营支出同比 +,invest_out_amt -- 投资支出 +,CASE WHEN invest_out_amt_yy = 0 THEN 0 + ELSE (invest_out_amt - invest_out_amt_yy) / invest_out_amt_yy END AS invest_out_amt_yy -- 投资支出同比 +,repay_out_amt -- 归还贷款 +,CASE WHEN repay_out_amt_yy = 0 THEN 0 + ELSE (repay_out_amt - repay_out_amt_yy) / repay_out_amt_yy END AS repay_out_amt_yy -- 归还贷款同比 +FROM calc_base +; + +-- 各项目收入 + +with rlt as ( + SELECT decode(trade_direction,'票据变现收入','支出',trade_direction) as trade_direction, + (CASE WHEN trade_direction = '收入' + then + (case when plan_item_name = '对外销售酒类回款' AND corp_code <> '0201' + then '成品酒销售回款' + when plan_item_name = '对外销售酒类回款' AND corp_code = '0201' + then '散酒销售回款' + when plan_item_name in ('保证金','共建基金') or (plan_item_name='其他收入(含未确认收入)' and trade_item IN ('红包充值费用','转货款')) + then '红包款/转货款/保证金/共建' + when plan_item_name IN('取得贷款','开票') + then '银行融资' + when plan_item_name IN('搬迁补偿/政府补助','政府返还及补助') + then '政府返还及补助' + when plan_item_name='票据变现收入' + then '其他(捐赠等)' + when plan_item_name IN('对外销售非酒类回款','吸收投资','税费返还','其他收入(含未确认收入)','利息收入') and nvl(trade_item,'x') not IN ('红包充值费用','转货款') + then '其他零星收款' end) + WHEN trade_direction = '支出' + then (case when plan_item_name in('原材料','辅料','低值易耗') + then '原辅低耗材料' + when plan_item_name in('散酒') + then '外购散酒' + when plan_item_name in('外购包材','外协加工费') + then '外购包材/外协加工' + when plan_item_name in('其它制造费用','水电气费') + then '制造费用/能源费用' + when plan_item_name in('广告费','市场活动投入','其它销售费用','委外服务(体验体系)','酒店采购','储运费') + then '广告/市场活动/庄园采购/储运' + when plan_item_name in('退共建基金') + then '共建支出' + when plan_item_name in('退经销商货款') + then '经销商退款' + when plan_item_name in('职工薪酬及福利') + then '薪酬及福利' + when plan_item_name in('税费') + then '税金缴纳' + when plan_item_name in('其它管理费用','财务手续费用','研发费用') + then '管理费用/研发费用' + when plan_item_name in ('土地支出','土建支出','利息支出','分配股利') + then plan_item_name + when plan_item_name in('设备支出','维修支出') + then '设备及维修支出' + when plan_item_name in('偿还贷款','开票兑付') + then '归还贷款' + else '其他(捐赠等)' end ) + else plan_item_name + end ) as fud_project_item, + + case when plan_item_name in ('票据变现收入','票据变现支出') + then '票据贴现费用' + when plan_item_name='其他收入(含未确认收入)' + then if(nvl(trade_item,'x') IN ('红包充值费用','转货款','窜货款'),trade_item,'其他') + else plan_item_name end as plan_item_name, + if(plan_item_name='其他收入(含未确认收入)' ,decode(trade_item,'转货款',12.1,'红包充值费用',12.2,'窜货款',12.3,st.sorted_no),nvl(st.sorted_no,99)) as srt, + SUM(CASE WHEN stat_date = '${sStart_date}' -- 当前统计时间 + THEN amt ELSE 0 END) / 10000 AS today_amt -- 本日 + ,SUM(CASE WHEN to_char(stat_date,'yyyy-MM') = '${sStart_mon}' -- 当前统计时间 + THEN amt ELSE 0 END) / 10000 AS month_amt -- 本月累计 + ,SUM(CASE WHEN to_char(stat_date,'yyyy') = '${sStart_year}' + THEN amt ELSE 0 END) / 10000 AS year_amt -- 本年累计 + + ,SUM(CASE WHEN stat_date = '${YEARDELTA(sStart_date,-1)}' -- 当前统计时间 + THEN amt ELSE 0 END) / 10000 AS last_year_day_amt -- 去年同期本日 + ,SUM(CASE WHEN to_char(stat_date,'yyyy-MM') = '${LEFT(YEARDELTA(CONCATENATE(sStart_mon,"-01"),-1),7)}' + THEN amt ELSE 0 END) / 10000 AS last_year_mon_amt -- 去年同期本月累计 + ,SUM(CASE WHEN to_char(stat_date,'yyyy') = '${YEAR(YEARDELTA(CONCATENATE(sStart_year,"-01-01"),-1))}' -- and stat_date <= date '${YEARDELTA(sStart_date,-1)}' + THEN amt ELSE 0 END) / 10000 AS last_year_amt -- 去年同期累计 + FROM dm.dm_fim_fud_indicator fp + left join sdi_xdata.mdm_dim_sorted st on fp.plan_item_name=st.sorted_value and st.sorted_type='资金项目' + where fp.trade_direction IN('收入','支出') + and fp.indi_type='执行' +-- and NVL(fp.owner_bank_account,'1') <> '22195101040012008' + AND fp.plan_item_name NOT IN ('内部调拨支出','内部调拨收入') + and EXISTS ( + SELECT + 1 + FROM + dm.dm_bi_user_permisson + WHERE + bi_user = '${fine_username}' + AND per_model IN ('all','fim') + AND ( + is_all = 1 + OR per_code = fp.corp_code + ) + ${IF(LEN(sCorp)!=0," AND fp.corp_code IN ('"+ sCorp +"')", "")} + ) + + AND stat_date >= date '${DATEINYEAR(YEARDELTA(sStart_date,-1),1)}' -- 上年年初 + -- and stat_date<= date '${sStart_date}' -- 当前统计时间 + group by + (CASE WHEN trade_direction = '收入' + then + (case when plan_item_name = '对外销售酒类回款' AND corp_code <> '0201' + then '成品酒销售回款' + when plan_item_name = '对外销售酒类回款' AND corp_code = '0201' + then '散酒销售回款' + when plan_item_name in ('保证金','共建基金') or (plan_item_name='其他收入(含未确认收入)' and trade_item IN ('红包充值费用','转货款')) + then '红包款/转货款/保证金/共建' + when plan_item_name IN('取得贷款','开票') + then '银行融资' + when plan_item_name IN('搬迁补偿/政府补助','政府返还及补助') + then '政府返还及补助' + when plan_item_name='票据变现收入' + then '其他(捐赠等)' + when plan_item_name IN('对外销售非酒类回款','吸收投资','税费返还','其他收入(含未确认收入)','利息收入') and nvl(trade_item,'x') not IN ('红包充值费用','转货款') + then '其他零星收款' end) + WHEN trade_direction = '支出' + then (case when plan_item_name in('原材料','辅料','低值易耗') + then '原辅低耗材料' + when plan_item_name in('散酒') + then '外购散酒' + when plan_item_name in('外购包材','外协加工费') + then '外购包材/外协加工' + when plan_item_name in('其它制造费用','水电气费') + then '制造费用/能源费用' + when plan_item_name in('广告费','市场活动投入','其它销售费用','委外服务(体验体系)','酒店采购','储运费') + then '广告/市场活动/庄园采购/储运' + when plan_item_name in('退共建基金') + then '共建支出' + when plan_item_name in('退经销商货款') + then '经销商退款' + when plan_item_name in('职工薪酬及福利') + then '薪酬及福利' + when plan_item_name in('税费') + then '税金缴纳' + when plan_item_name in('其它管理费用','财务手续费用','研发费用') + then '管理费用/研发费用' + when plan_item_name in ('土地支出','土建支出','利息支出','分配股利') + then plan_item_name + when plan_item_name in('设备支出','维修支出') + then '设备及维修支出' + when plan_item_name in('偿还贷款','开票兑付') + then '归还贷款' + else '其他(捐赠等)' end ) + else plan_item_name + end ), + case when plan_item_name in ('票据变现收入','票据变现支出') + then '票据贴现费用' + when plan_item_name='其他收入(含未确认收入)' + then if(nvl(trade_item,'x') IN ('红包充值费用','转货款','窜货款'),trade_item,'其他') + else plan_item_name end, + if(plan_item_name='其他收入(含未确认收入)' ,decode(trade_item,'转货款',12.1,'红包充值费用',12.2,'窜货款',12.3,st.sorted_no),nvl(st.sorted_no,99)), + decode(trade_direction,'票据变现收入','支出',trade_direction) + union all + SELECT + '收入' as trade_direction, + '其他零星收款' as fud_project_item, + '汇兑损益' as plan_item_name, + 99 as srt, + SUM(CASE WHEN last_day(date(concat_ws(',',account_year,account_month,'1'))) = '${sStart_date}' -- 当前统计时间 + THEN losses_amt ELSE 0 END) / 10000 AS today_amt -- 本日 + ,SUM(CASE WHEN to_char(date(concat_ws(',',account_year,account_month,'1')),'yyyy-MM') = '${sStart_mon}' -- 当前统计时间 + THEN losses_amt ELSE 0 END) / 10000 AS month_amt -- 本月累计 + ,SUM(CASE WHEN to_char(date(concat_ws(',',account_year,account_month,'1')),'yyyy') = '${sStart_year}' -- 当前统计时间 + THEN losses_amt ELSE 0 END) / 10000 AS year_amt -- 本年累计 + + ,SUM(CASE WHEN last_day(date(concat_ws(',',account_year,account_month,'1'))) = '${YEARDELTA(sStart_date,-1)}' + THEN losses_amt ELSE 0 END) / 10000 AS last_year_day_amt -- 去年同期本日累计 + ,SUM(CASE WHEN to_char(date(concat_ws(',',account_year,account_month,'1')),'yyyy-MM') = '${LEFT(YEARDELTA(CONCATENATE(sStart_mon,"-01"),-1),7)}' + THEN losses_amt ELSE 0 END) / 10000 AS last_year_mon_amt -- 去年同期本月累计 + ,SUM(CASE WHEN to_char(date(concat_ws(',',account_year,account_month,'1')),'yyyy') = '${YEAR(YEARDELTA(CONCATENATE(sStart_year,"-01-01"),-1))}' + -- and date(concat_ws(',',account_year,account_month,'1'))<=date'${YEARDELTA(sStart_date,-1)}' + THEN losses_amt ELSE 0 END) / 10000 AS last_year_amt -- 去年同期累计 + FROM sdi_hzg.exchange_gains_losses fp + where + EXISTS ( + SELECT + 1 + FROM + dm.dm_bi_user_permisson + WHERE + bi_user = '${fine_username}' + AND per_model IN ('all','fim') + AND ( + is_all = 1 + OR per_code = fp.corp_code + ) + ) + ${IF(LEN(sCorp)!=0," AND fp.corp_code IN ('"+ sCorp +"')", "")} + + AND date(concat_ws(',',account_year,account_month,'1')) >= date '${DATEINYEAR(YEARDELTA(sStart_date,-1),1)}' -- 上年年初 + -- and date(concat_ws(',',account_year,account_month,'1'))<= date '${sStart_date}' -- 当前统计时间 +) +SELECT + fud_project_item + ,SUM(${switch(sData_type,'0',"today_amt",'1',"month_amt",'2',"year_amt")}) AS in_amt -- 酒类回款本期收入 + ,SUM(${switch(sData_type,'0',"last_year_day_amt",'1',"last_year_mon_amt",'2',"last_year_amt")}) AS in_amt_yy -- 酒类回款同期收入 +FROM rlt +WHERE trade_direction = '收入' +GROUP BY fud_project_item +ORDER BY SUM(${switch(sData_type,'0',"today_amt",'1',"month_amt",'2',"year_amt")}) +; +-- 收入趋势 + + +with rlt as (SELECT DECODE(trade_direction, '票据变现收入', '支出', trade_direction) AS trade_direction + ,stat_date + ,amt + FROM dm.dm_fim_fud_indicator fp + LEFT JOIN sdi_xdata.mdm_dim_sorted st + ON fp.plan_item_name = st.sorted_value AND st.sorted_type = '资金项目' + WHERE fp.trade_direction IN ('收入', '支出') + AND fp.indi_type = '执行' +-- and NVL(fp.owner_bank_account,'1') <> '22195101040012008' + AND fp.plan_item_name NOT IN ('内部调拨支出', '内部调拨收入') + AND EXISTS (SELECT 1 + FROM dm.dm_bi_user_permisson + WHERE bi_user = '${fine_username}' + AND per_model IN ('all', 'fim') + AND ( + is_all = 1 + OR per_code = fp.corp_code + ) + ${IF(LEN(sCorp)!=0," AND fp.corp_code IN ('"+ sCorp +"')", "")}) + UNION ALL + SELECT '收入' AS trade_direction + ,last_day(date(CONCAT_WS(',', account_year, account_month, '1'))) as stat_date + ,losses_amt AS amt + FROM sdi_hzg.exchange_gains_losses fp + WHERE EXISTS(SELECT 1 + FROM + dm.dm_bi_user_permisson + WHERE + bi_user = '${fine_username}' + AND per_model IN ('all','fim') + AND ( + is_all = 1 + OR per_code = fp.corp_code + ) + ) + ${IF(LEN(sCorp)!=0," AND fp.corp_code IN ('"+ sCorp +"')", "")} + +), +date_series AS( +-- 生成时间序列 + select + stat_date AS stat_date + ,EXTRACT(MONTH FROM stat_date) AS stat_mon + , EXTRACT(DAY FROM stat_date) AS stat_day + ,EXTRACT(YEAR FROM stat_date) AS stat_year + from generate_series((DATE_TRUNC('year', CURRENT_DATE) - INTERVAL '1 year')::DATE ,CURRENT_DATE,'1 day') stat_date + + +), +calc_base AS ( + SELECT + A.stat_date AS stat_date + ,coalesce(B.amt,0) AS amt + ,coalesce(C.amt,0) AS amt_yy + + + FROM date_series A + LEFT JOIN + ( + SELECT + stat_date + ,SUM(amt) AS amt + FROM rlt + WHERE trade_direction = '收入' + GROUP BY stat_date + )B + ON A.stat_mon = EXTRACT(MONTH FROM B.stat_date) + AND A.stat_day = EXTRACT(DAY FROM B.stat_date) + AND A.stat_year = EXTRACT(YEAR FROM B.stat_date) + LEFT JOIN + ( + SELECT + stat_date + ,SUM(amt) AS amt + FROM rlt + WHERE trade_direction = '收入' + GROUP BY stat_date + )C + ON A.stat_mon = EXTRACT(MONTH FROM C.stat_date) + AND A.stat_day = EXTRACT(DAY FROM C.stat_date) + AND A.stat_year = EXTRACT(YEAR FROM C.stat_date) + 1 +) + + -- 近15天 +${if(sStat_type=1,"","/*")} +select +to_char(stat_date,'MM-dd') AS stat_date +,to_char(stat_date,'yyyy-MM-dd') AS stat_time +,SUM(amt) * (1/10000) AS amt +,SUM(amt_yy) * (1/10000) AS amt_yy +from calc_base +WHERE to_char(stat_date,'yyyy-MM-dd') >= '${FORMAT(DATEDELTA(TODAY(),-15),"yyyy-MM-dd")}' +AND to_char(stat_date,'yyyy-MM-dd') <= '${FORMAT(TODAY(),"yyyy-MM-dd")}' +GROUP BY to_char(stat_date,'MM-dd'),to_char(stat_date,'yyyy-MM-dd') +ORDER BY to_char(stat_date,'yyyy-MM-dd') +${if(sStat_type=1,"","*/")} + +-- 近12个月 + ${if(sStat_type=2,"","/*")} +select + to_char(stat_date,'yyyyMM') AS stat_date + ,to_char(stat_date,'yyyyMM') AS stat_time + ,SUM(amt) * (1/10000) AS amt + ,SUM(amt_yy) * (1/10000) AS amt_yy +from calc_base +WHERE to_char(stat_date,'yyyy-MM') >= '${FORMAT(MONTHDELTA(TODAY(),-12),"yyyy-MM")}' +AND to_char(stat_date,'yyyy-MM') <= '${FORMAT(TODAY(),"yyyy-MM")}' +GROUP BY to_char(stat_date,'yyyyMM') +ORDER BY to_char(stat_date,'yyyyMM') +${if(sStat_type=2,"","*/")} + + +-- 各项目支出 +-- 数据源来自BI报表【资金计划执行】 + +with tb as (select a.stat_date, + c.corp_short_name, + a.trade_direction, + a.indi_type, + a.plan_item_class, + a.plan_item_name, + a.trade_item, + a.amt + from dm.dm_fim_fud_indicator a + inner join dwr.dim_corp c on a.corp_code=c.corp_code and c.corp_group='股份' + where a.indi_type = '执行' + -- ${if(sStatisticCaliber="年度预算执行","","and if(a.plan_item_name in ('内部调拨收入','内部调拨支出') and a.indi_type='计划',0,1)=1")} + + ${IF(len(sCorp)=0," ","AND a.corp_code in ('"+sCorp+"')")} + AND EXISTS ( + SELECT + 1 + FROM + dm.dm_bi_user_permisson + WHERE + bi_user = '${fine_username}' -- 替换为fine_username + AND per_model IN ('all','fim') + AND ( + is_all = 1 + OR per_code = a.corp_code + ) + ) + +) + ,tmp as (select + trade_direction, + indi_type, + stat_date, + YEAR(stat_date) AS year, + MONTH(stat_date) AS month, + corp_short_name, + case when plan_item_name in ('取得贷款','内部调拨收入','票据变现收入','出售房屋及构筑物','吸收投资','投资收益','收回投资') and trade_direction='收入' then '非经营活动' + when plan_item_name in ('偿还贷款','土地支出','土建支出','设备支出','维修支出','分配股利','利息支出','购买房屋及构筑物','投资支出','票据变现支出','内部调拨支出','筹资费用') and trade_direction='支出' then '非经营活动' + else '经营活动' + end as item_type, + case when plan_item_name in ('偿还贷款','土地支出','土建支出','设备支出','维修支出','分配股利','利息支出','购买房屋及建筑物','投资支出','票据变现支出','筹资费用') then '投资筹资类' + when plan_item_name in ('取得贷款','票据变现收入','出售房屋及建筑物','吸收投资','投资收益','收回投资') then '投资筹资类' + else '非投资筹资类' + end as item_type1, + if(plan_item_name='其他收入(含未确认收入)' ,if(trade_item in ('窜货款', '红包充值费用', '转货款'),trade_item,'其他'),plan_item_name) as plan_item_name, + sum(amt) * (1 / 10000) AS amt + from tb + where 1=1 + and stat_date >= date'${switch(sData_type,'0',DATEINYEAR(YEARDELTA(sStart_date,-1),1),'1',DATEINYEAR(YEARDELTA(CONCATENATE(sStart_mon,"-01"),-1),1),'2',DATEINYEAR(YEARDELTA(CONCATENATE(sStart_year,"-01-01"),-1),1))}' + + -- and stat_date <= date '${sStart_date}' + -- ${if(sData_type='2',"AND stat_date<=date('"+CONCATENATE(ENDOFMONTH(sStart_date)," 23:59:59")+"')","AND MONTH(stat_date)="+MONTH(sStart_date)+"")} + group by + trade_direction, + indi_type, + stat_date, + YEAR(stat_date), + MONTH(stat_date), + corp_short_name, + case when plan_item_name in ('取得贷款','内部调拨收入','票据变现收入','出售房屋及构筑物','吸收投资','投资收益','收回投资') and trade_direction='收入' then '非经营活动' + when plan_item_name in ('偿还贷款','土地支出','土建支出','设备支出','维修支出','分配股利','利息支出','购买房屋及构筑物','投资支出','票据变现支出','内部调拨支出','筹资费用') and trade_direction='支出' then '非经营活动' + else '经营活动' + end, + case when plan_item_name in ('偿还贷款','土地支出','土建支出','设备支出','维修支出','分配股利','利息支出','购买房屋及建筑物','投资支出','票据变现支出','筹资费用') then '投资筹资类' + when plan_item_name in ('取得贷款','票据变现收入','出售房屋及建筑物','吸收投资','投资收益','收回投资') then '投资筹资类' + else '非投资筹资类' + end, + if(plan_item_name='其他收入(含未确认收入)' ,if(trade_item in ('窜货款', '红包充值费用', '转货款'),trade_item,'其他'),plan_item_name) +) + + + +SELECT +A.plan_item_class AS plan_item_class +,SUM(B.支出执行) AS 支出执行 +,SUM(B.支出执行同期) AS 支出执行同期 +FROM +( + SELECT fp.plan_item_class, + plan_item_name + FROM dm.dm_fim_fud_indicator fp + LEFT JOIN sdi_xdata.mdm_dim_sorted st ON st.sorted_value = fp.plan_item_name AND st.sorted_type = '资金项目' + WHERE fp.trade_direction = '支出' + -- AND stat_date >= date'${CONCATENATE(YEAR(sStart_date),"-01-01")}' + AND EXISTS(SELECT 1 + FROM + dm.dm_bi_user_permisson + WHERE + bi_user = '${fine_username}' -- 替换为${fine_username} + AND per_model IN ('all','fim') + AND ( + is_all = 1 + OR per_code = fp.corp_code + ) + ) + ${IF(len(sCorp)=0," ","AND fp.corp_code in ('"+sCorp+"')")} + GROUP BY fp.plan_item_class, + plan_item_name, + st.sorted_no +-- order by nvl(st.sorted_no,99) +)A + JOIN +( + select + trade_direction, + corp_short_name, + plan_item_name, + SUM(CASE WHEN ${switch(sData_type,'0',"stat_date >= date '" + sStart_date + "' AND stat_date <= date '" + sStart_date + "'",'1',"to_char(stat_date,'yyyy-MM') = '" + sStart_mon + "'",'2',"to_char(stat_date,'yyyy') = '" + sStart_year + "' AND to_char(stat_date,'yyyy-MM') <= '" + sStart_mon + "'")} then amt else 0 end) as "支出执行", + SUM(CASE WHEN ${switch(sData_type,'0',"stat_date >= date '" + YEARDELTA(sStart_date,-1) + "' AND stat_date <= date '" + YEARDELTA(sStart_date,-1) + "'",'1',"to_char(stat_date,'yyyy-MM') = '" + LEFT(YEARDELTA(CONCATENATE(sStart_mon,"-01"),-1),7) + "'",'2',"to_char(stat_date,'yyyy') = '" + YEAR(YEARDELTA(CONCATENATE(sStart_year,"-01-01"),-1)) + "' AND to_char(stat_date,'yyyy-MM') <= '" + LEFT(YEARDELTA(CONCATENATE(sStart_mon,"-01"),-1),7) + "'")} then amt else 0 end) as "支出执行同期" + FROM tmp + WHERE trade_direction = '支出' + AND indi_type = '执行' + AND plan_item_name <> '内部调拨支出' + GROUP BY + trade_direction, + indi_type, + year, + month, + corp_short_name, + plan_item_name +)B +ON A.plan_item_name = B.plan_item_name +GROUP BY A.plan_item_class +ORDER BY SUM(B.支出执行) +; + +-- 支出趋势 +-- 取自【资金收支日报】 + + +with rlt as (SELECT DECODE(trade_direction, '票据变现收入', '支出', trade_direction) AS trade_direction + ,stat_date + ,amt + FROM dm.dm_fim_fud_indicator fp + LEFT JOIN sdi_xdata.mdm_dim_sorted st + ON fp.plan_item_name = st.sorted_value AND st.sorted_type = '资金项目' + WHERE fp.trade_direction IN ('收入', '支出') + AND fp.indi_type = '执行' +-- and NVL(fp.owner_bank_account,'1') <> '22195101040012008' + AND fp.plan_item_name NOT IN ('内部调拨支出', '内部调拨收入') + AND EXISTS (SELECT 1 + FROM dm.dm_bi_user_permisson + WHERE bi_user = '${fine_username}' + AND per_model IN ('all', 'fim') + AND ( + is_all = 1 + OR per_code = fp.corp_code + ) + ${IF(LEN(sCorp)!=0," AND fp.corp_code IN ('"+ sCorp +"')", "")}) + UNION ALL + SELECT '收入' AS trade_direction + ,last_day(date(CONCAT_WS(',', account_year, account_month, '1'))) as stat_date + ,losses_amt AS amt + FROM sdi_hzg.exchange_gains_losses fp + WHERE EXISTS(SELECT 1 + FROM + dm.dm_bi_user_permisson + WHERE + bi_user = '${fine_username}' + AND per_model IN ('all','fim') + AND ( + is_all = 1 + OR per_code = fp.corp_code + ) + ) + ${IF(LEN(sCorp)!=0," AND fp.corp_code IN ('"+ sCorp +"')", "")} + +), + date_series AS( +-- 生成时间序列 + select + stat_date AS stat_date + ,EXTRACT(MONTH FROM stat_date) AS stat_mon + , EXTRACT(DAY FROM stat_date) AS stat_day + ,EXTRACT(YEAR FROM stat_date) AS stat_year + from generate_series((DATE_TRUNC('year', CURRENT_DATE) - INTERVAL '1 year')::DATE ,CURRENT_DATE,'1 day') stat_date + + +), +calc_base AS ( + SELECT + A.stat_date AS stat_date + ,coalesce(B.amt,0) AS amt + ,coalesce(C.amt,0) AS amt_yy + + + FROM date_series A + LEFT JOIN + ( + SELECT + stat_date + ,SUM(amt) AS amt + FROM rlt + WHERE trade_direction = '支出' + GROUP BY stat_date + )B + ON A.stat_mon = EXTRACT(MONTH FROM B.stat_date) + AND A.stat_day = EXTRACT(DAY FROM B.stat_date) + AND A.stat_year = EXTRACT(YEAR FROM B.stat_date) + LEFT JOIN + ( + SELECT + stat_date + ,SUM(amt) AS amt + FROM rlt + WHERE trade_direction = '支出' + GROUP BY stat_date + )C + ON A.stat_mon = EXTRACT(MONTH FROM C.stat_date) + AND A.stat_day = EXTRACT(DAY FROM C.stat_date) + AND A.stat_year = EXTRACT(YEAR FROM C.stat_date) + 1 +) + + -- 近15天 + ${if(sStat_type1=1,"","/*")} +select + to_char(stat_date,'MM-dd') AS stat_date + ,to_char(stat_date,'yyyy-MM-dd') AS stat_time + ,SUM(amt) * (1/10000) AS amt + ,SUM(amt_yy) * (1/10000) AS amt_yy +from calc_base +WHERE to_char(stat_date,'yyyy-MM-dd') >= '${FORMAT(DATEDELTA(TODAY(),-15),"yyyy-MM-dd")}' + AND to_char(stat_date,'yyyy-MM-dd') <= '${FORMAT(TODAY(),"yyyy-MM-dd")}' +GROUP BY to_char(stat_date,'MM-dd'),to_char(stat_date,'yyyy-MM-dd') +ORDER BY to_char(stat_date,'yyyy-MM-dd') + ${if(sStat_type1=1,"","*/")} + +-- 近12个月 + ${if(sStat_type1=2,"","/*")} +select + to_char(stat_date,'yyyyMM') AS stat_date + ,to_char(stat_date,'yyyyMM') AS stat_time + ,SUM(amt) * (1/10000) AS amt + ,SUM(amt_yy) * (1/10000) AS amt_yy +from calc_base +WHERE to_char(stat_date,'yyyy-MM') >= '${FORMAT(MONTHDELTA(TODAY(),-12),"yyyy-MM")}' + AND to_char(stat_date,'yyyy-MM') <= '${FORMAT(TODAY(),"yyyy-MM")}' +GROUP BY to_char(stat_date,'yyyyMM') +ORDER BY to_char(stat_date,'yyyyMM') +${if(sStat_type1=2,"","*/")} + + +-- 资金余额类型 + +with dba as ( + select decode(length(accountnumber), '17', + substring(accountnumber, 3), + accountnumber) as accountnumber, + max(category_name) as category_name + + from dwr.dim_bank_account + where account_owner_class='langjiu' + group by decode(length(accountnumber), '17', + substring(accountnumber, 3), + accountnumber) +), + lmt as (/*用于计算受限资金*/ + select + report_date, + corp_code, + bank_account, + currency, + /*20351059900100000512291 为受限资金账户*/ + current_balance-decode(bank_account,'20351059900100000512291',0,usable_balance)-nvl(lag(current_balance-decode(bank_account,'20351059900100000512291',0,usable_balance),1) over (partition by bank_account,currency order by report_date),0) as limit_amt_pre + from dwi.dwi_fim_bank_account_balance + ), ye as ( + + select corp_code, + owner_bank_account, + amt, + amt_type, + decode(is_usable,1,0,1) as is_usable + from dm.dm_fim_fud_balance + where stat_date=date'${DATEINMONTH(MONTHDELTA(sStart_date,-1),1)}' + union all + /*汇兑损益*/ + SELECT + corp_code, + null as owner_bank_account, + losses_amt as amt, + '现金' as amt_type, + 0 as is_usable + FROM sdi_hzg.exchange_gains_losses + -- where date(concat_ws(',',account_year,account_month,'1'))=date'${DATEINMONTH(sStart_date,1)}' + -- ${switch(sData_type,'0',"date(concat_ws(',',account_year,account_month,'1')) = '" + DATEINMONTH(sStart_date,1) + "'",'1',"to_char(date(concat_ws(',',account_year,account_month,'1')),'yyyy-MM') = '" + sStart_mon + "'",'2',"to_char(stat_date,'yyyy') = '" + sStart_year + "' AND to_char(stat_date,'yyyy-MM') <= '" + sStart_mon + "'")} + + union all + select + corp_code, + owner_bank_account, + sum(decode(trade_direction,'支出',-1*amt,amt)) as amt, + amt_type, + 0 as is_usable + from dm.dm_fim_fud_indicator + where indi_type='执行' + and stat_date>=date'${switch(sData_type,"0",DATEINMONTH(sStart_date,1),"1",CONCATENATE(sStart_mon,"-01"),"2",CONCATENATE(sStart_year,"-01-01"))}' + and stat_date<=date'${switch(sData_type,"0",sStart_date,"1",ENDOFMONTH(CONCATENATE(sStart_mon,"-01")),"2",CONCATENATE(sStart_year,"12-31"))}' + --and plan_item_name not in ('内部调拨收入', '内部调拨支出') + group by corp_code, + owner_bank_account, + amt_type + union all +/*受限资金*/ + select corp_code, + bank_account as owner_bank_account, + sum(limit_amt_pre) as amt, + '现金' as amt_type, + 1 as is_usable + from lmt + where limit_amt_pre!=0 + and report_date>=date'${switch(sData_type,"0",DATEINMONTH(sStart_date,1),"1",CONCATENATE(sStart_mon,"-01"),"2",CONCATENATE(sStart_year,"-01-01"))}' + and report_date<=date'${switch(sData_type,"0",sStart_date,"1",ENDOFMONTH(CONCATENATE(sStart_mon,"-01")),"2",CONCATENATE(sStart_year,"12-31"))}' + group by corp_code,bank_account + union all +/*受限资金*/ + select corp_code, + bank_account as owner_bank_account, + -1*sum(limit_amt_pre) as amt, + '现金' as amt_type, + 0 as is_usable + from lmt + where limit_amt_pre!=0 + and report_date>=date'${switch(sData_type,"0",DATEINMONTH(sStart_date,1),"1",CONCATENATE(sStart_mon,"-01"),"2",CONCATENATE(sStart_year,"-01-01"))}' + and report_date<=date'${switch(sData_type,"0",sStart_date,"1",ENDOFMONTH(CONCATENATE(sStart_mon,"-01")),"2",CONCATENATE(sStart_year,"12-31"))}' + group by corp_code,bank_account +), +base_calc AS (SELECT CASE + WHEN a.is_usable = 0 AND a.amt_type = '现金' AND a.corp_code <> '0223' + THEN '其中:可用存款' + WHEN a.amt_type = '票据' + THEN '银行承兑汇票' + WHEN a.amt_type = '现金' AND a.corp_code = '0223' + THEN '香港公司账户资金' + WHEN a.is_usable = 1 AND a.amt_type = '现金' AND a.corp_code <> '0223' + THEN '受限资金' + END AS subject_type + , SUM(a.amt) * (1 / 10000) AS yb_balance + +-- is_usable as is_limit--受限资金金额 + FROM ye a + INNER JOIN dwr.dim_corp c ON a.corp_code = c.corp_code AND c.corp_group = '股份' +/*left join dba on decode(length(a.owner_bank_account), 17, + substring(a.owner_bank_account, 3), + a.owner_bank_account)=dba.accountnumber*/ + WHERE EXISTS (SELECT 1 + FROM dm.dm_bi_user_permisson + WHERE bi_user = '${fine_username}' + AND per_model IN ('all', 'fim') + AND ( + is_all = 1 + )) + AND a.amt != 0 + ${IF(LEN(sCorp)!=0," AND a.corp_code IN ('"+ sCorp +"')", "")} + GROUP BY CASE WHEN a.is_usable = 0 AND a.amt_type = '现金' AND a.corp_code <> '0223' + THEN '其中:可用存款' + WHEN a.amt_type = '票据' + THEN '银行承兑汇票' + WHEN a.amt_type = '现金' AND a.corp_code = '0223' + THEN '香港公司账户资金' + WHEN a.is_usable = 1 AND a.amt_type = '现金' AND a.corp_code <> '0223' + THEN '受限资金' END +) +SELECT + SUM(CASE WHEN A.subject_type in('其中:可用存款','香港公司账户资金') THEN A.yb_balance ELSE 0 END) AS bank_amt -- 银行存款 + ,SUM(CASE WHEN A.subject_type in('银行承兑汇票') THEN A.yb_balance ELSE 0 END) AS bill_amt -- 票据 + ,SUM(CASE WHEN A.subject_type in('受限资金') THEN A.yb_balance ELSE 0 END) AS limt_amt -- 受限资金 +FROM base_calc A + +; + + +-- 资金分析-银行明细 + + +with dba as (/*存在一个账户多行数据,币种不同*/ + select accountnumber,bank_name from dwr.dim_bank_account + where account_owner_class='langjiu' + group by accountnumber,bank_name +) +SELECT + -- c.corp_name, + b.bank_name, + -- a.owner_bank_account, + -- sum(case when amt_type = '现金' then amt end) * (1/10000) AS 现金余额, + sum(case when amt_type = '现金' and is_usable = '1' then amt end) * (1/10000) AS 现金可用余额, -- 银行存款 + sum(case when amt_type = '票据' then amt end) * (1/10000) AS 票据余额, -- 票据 + -- sum(case when amt_type = '票据' and is_usable = '1' then amt end) AS 票据可用余额, + (sum(case when amt_type = '现金' then amt end) - sum(case when amt_type = '现金' and is_usable = '1' then amt end)) * (1/10000) AS 受限资金 +FROM dm.dm_fim_fud_balance a +LEFT JOIN dba b ON decode(length(a.owner_bank_account), 17,substring(a.owner_bank_account, 3),a.owner_bank_account)=decode(length(b.accountnumber), '17', substring(b.accountnumber, 3),b.accountnumber) +LEFT JOIN dwi.dwi_mdm_corp c ON a.corp_code = c.corp_code +WHERE + exists ( + select + 1 + from + dm.dm_bi_user_permisson + where + bi_user = '${fine_username}' + and per_model IN ('all','fim') + and ( + is_all = 1 + OR per_code = a.corp_code + ) + ) + + -- AND to_char(a.stat_date,'yyyy') = '${sStart_year}' + -- AND month(a.stat_date) = '${sStart_mon}' + ${if(len(sCorp) == 0,"","and a.corp_code IN ('"+sCorp+"')")} + ${switch(sData_type,'0',"AND to_char(stat_date,'yyyy-MM') = '" + sStart_mon + "'",'1',"AND to_char(stat_date,'yyyy-MM') = '" + sStart_mon + "'",'2',"AND to_char(stat_date,'yyyy') = '" + sStart_year + "'")} + +GROUP BY + -- c.corp_name, + b.bank_name + -- a.owner_bank_account +HAVING sum(amt)!=0 +ORDER BY sum(case when amt_type = '现金' and is_usable = '1' then amt end) DESC + +; + +-- 资金分析-资金余额趋势 + +-- 资金余额类型 + +with dba as ( + select decode(length(accountnumber), '17', + substring(accountnumber, 3), + accountnumber) as accountnumber, + max(category_name) as category_name + + from dwr.dim_bank_account + where account_owner_class='langjiu' + group by decode(length(accountnumber), '17', + substring(accountnumber, 3), + accountnumber) +), +lmt as (/*用于计算受限资金*/ + select + report_date, + corp_code, + bank_account, + currency, + /*20351059900100000512291 为受限资金账户*/ + current_balance-decode(bank_account,'20351059900100000512291',0,usable_balance)-nvl(lag(current_balance-decode(bank_account,'20351059900100000512291',0,usable_balance),1) over (partition by bank_account,currency order by report_date),0) as limit_amt_pre + from dwi.dwi_fim_bank_account_balance + ), ye as ( + + select corp_code, + owner_bank_account, + amt, + amt_type, + decode(is_usable,1,0,1) as is_usable + from dm.dm_fim_fud_balance + where stat_date=date'${DATEINMONTH(MONTHDELTA(sStart_date,-1),1)}' + union all + /*汇兑损益*/ + SELECT + corp_code, + null as owner_bank_account, + losses_amt as amt, + '现金' as amt_type, + 0 as is_usable + FROM sdi_hzg.exchange_gains_losses + -- where date(concat_ws(',',account_year,account_month,'1'))=date'${DATEINMONTH(sStart_date,1)}' + -- ${switch(sData_type,'0',"date(concat_ws(',',account_year,account_month,'1')) = '" + DATEINMONTH(sStart_date,1) + "'",'1',"to_char(date(concat_ws(',',account_year,account_month,'1')),'yyyy-MM') = '" + sStart_mon + "'",'2',"to_char(stat_date,'yyyy') = '" + sStart_year + "' AND to_char(stat_date,'yyyy-MM') <= '" + sStart_mon + "'")} + + union all + select + corp_code, + owner_bank_account, + sum(decode(trade_direction,'支出',-1*amt,amt)) as amt, + amt_type, + 0 as is_usable + from dm.dm_fim_fud_indicator + where indi_type='执行' + and stat_date>=date'${switch(sData_type,"0",DATEINMONTH(sStart_date,1),"1",CONCATENATE(sStart_mon,"-01"),"2",CONCATENATE(sStart_year,"-01-01"))}' + and stat_date<=date'${switch(sData_type,"0",sStart_date,"1",ENDOFMONTH(CONCATENATE(sStart_mon,"-01")),"2",CONCATENATE(sStart_year,"12-31"))}' + --and plan_item_name not in ('内部调拨收入', '内部调拨支出') + group by corp_code, + owner_bank_account, + amt_type + union all +/*受限资金*/ + select corp_code, + bank_account as owner_bank_account, + sum(limit_amt_pre) as amt, + '现金' as amt_type, + 1 as is_usable + from lmt + where limit_amt_pre!=0 + and report_date>=date'${switch(sData_type,"0",DATEINMONTH(sStart_date,1),"1",CONCATENATE(sStart_mon,"-01"),"2",CONCATENATE(sStart_year,"-01-01"))}' + and report_date<=date'${switch(sData_type,"0",sStart_date,"1",ENDOFMONTH(CONCATENATE(sStart_mon,"-01")),"2",CONCATENATE(sStart_year,"12-31"))}' + group by corp_code,bank_account + union all +/*受限资金*/ + select corp_code, + bank_account as owner_bank_account, + -1*sum(limit_amt_pre) as amt, + '现金' as amt_type, + 0 as is_usable + from lmt + where limit_amt_pre!=0 + and report_date>=date'${switch(sData_type,"0",DATEINMONTH(sStart_date,1),"1",CONCATENATE(sStart_mon,"-01"),"2",CONCATENATE(sStart_year,"-01-01"))}' + and report_date<=date'${switch(sData_type,"0",sStart_date,"1",ENDOFMONTH(CONCATENATE(sStart_mon,"-01")),"2",CONCATENATE(sStart_year,"12-31"))}' + group by corp_code,bank_account +), + base_calc AS (SELECT CASE + WHEN a.is_usable = 0 AND a.amt_type = '现金' AND a.corp_code <> '0223' + THEN '其中:可用存款' + WHEN a.amt_type = '票据' + THEN '银行承兑汇票' + WHEN a.amt_type = '现金' AND a.corp_code = '0223' + THEN '香港公司账户资金' + WHEN a.is_usable = 1 AND a.amt_type = '现金' AND a.corp_code <> '0223' + THEN '受限资金' + END AS subject_type + , SUM(a.amt) * (1 / 10000) AS yb_balance + +-- is_usable as is_limit--受限资金金额 + FROM ye a + INNER JOIN dwr.dim_corp c ON a.corp_code = c.corp_code AND c.corp_group = '股份' +/*left join dba on decode(length(a.owner_bank_account), 17, + substring(a.owner_bank_account, 3), + a.owner_bank_account)=dba.accountnumber*/ + WHERE EXISTS (SELECT 1 + FROM dm.dm_bi_user_permisson + WHERE bi_user = '${fine_username}' + AND per_model IN ('all', 'fim') + AND ( + is_all = 1 + )) + AND a.amt != 0 + ${IF(LEN(sCorp)!=0," AND a.corp_code IN ('"+ sCorp +"')", "")} + GROUP BY CASE WHEN a.is_usable = 0 AND a.amt_type = '现金' AND a.corp_code <> '0223' + THEN '其中:可用存款' + WHEN a.amt_type = '票据' + THEN '银行承兑汇票' + WHEN a.amt_type = '现金' AND a.corp_code = '0223' + THEN '香港公司账户资金' + WHEN a.is_usable = 1 AND a.amt_type = '现金' AND a.corp_code <> '0223' + THEN '受限资金' END + ) +SELECT + + SUM(CASE WHEN A.subject_type in('其中:可用存款','香港公司账户资金') THEN A.yb_balance ELSE 0 END) AS bank_amt -- 银行存款 + ,SUM(CASE WHEN A.subject_type in('银行承兑汇票') THEN A.yb_balance ELSE 0 END) AS bill_amt -- 票据 + ,SUM(CASE WHEN A.subject_type in('受限资金') THEN A.yb_balance ELSE 0 END) AS limt_amt -- 受限资金 +FROM base_calc A +; + + + +-- 银行负债-银行负债余额类型 + +-- 贷款余额 较年初增长 同比增长额 +-- 票据余额 较年初增长 同比增长额 + +with data_dk as( + select + bank_code + ,corp_code as org_code + ,occupy_credit_quota_type + ,sum(case when to_char(case when actual_amort_day is not null then actual_amort_day else term_day end::date,'yyyy-MM-dd') > sysdate and to_char(loan_day::date,'yyyy-MM-dd') <= sysdate then term_amt else 0 end) this_amt -- 贷款金额 + ,sum(case when to_char(case when actual_amort_day is not null then actual_amort_day else term_day end::date,'yyyy-MM-dd') > (DATE_TRUNC('year', sysdate) - INTERVAL '1 day')::DATE and to_char(loan_day::date,'yyyy-MM-dd') <= (DATE_TRUNC('year', sysdate) - INTERVAL '1 day')::DATE then term_amt else 0 end) year_amt -- 年初合计 + ,sum(case when to_char(loan_day::date,'yyyy-MM-dd') <= sysdate::date - interval '1 year' then term_amt else 0 end) last_amt -- 去年同期合计 + from + sdi_xdata.fim_liab_amort_plan A + where -- 优先获取实际还款日期,再取到期日期 + to_char(case when actual_amort_day is not null then actual_amort_day else term_day end::date,'yyyy-MM-dd') > sysdate::date - interval '1 year' -- 去年同期的日期 + and to_char(loan_day::date,'yyyy-MM-dd') <= sysdate::date -- 借款日期要在查询日期之前的 + group by + bank_code, + corp_code , + occupy_credit_quota_type +), + data_yf as ( + select + a.draw_bank_code --出票人银行 + ,a.corp_code + ,'应付票据金额' as occupy_credit_quota_type + ,sum(case when to_char(end_date,'yyyy-MM-dd') > sysdate::date and to_char(issue_date,'yyyy-MM-dd') <= sysdate::date then a.amt else 0 end) this_amt -- 票面金额 + ,sum(case when to_char(end_date,'yyyy-MM-dd') > (DATE_TRUNC('year', sysdate) - INTERVAL '1 day')::DATE and to_char(issue_date,'yyyy-MM-dd') <= (DATE_TRUNC('year', sysdate) - INTERVAL '1 day')::DATE then a.amt else 0 end) year_amt -- 年初合计 + ,sum(case when to_char(issue_date,'yyyy-MM-dd') <= sysdate::date - interval '1 year' then a.amt else 0 end) last_amt -- 去年同期 + from + dwr.fact_fim_draft_flow a + where + operate_flag='1003' -- 开票 + and to_char(end_date,'yyyy-MM-dd') > sysdate::date - interval '1 year' -- 到期日期 + and to_char(issue_date,'yyyy-MM-dd') <= sysdate::date -- 出票日期 + group by + a.corp_code,a.draw_bank_code + order by + a.corp_code,a.draw_bank_code + ), + dz as ( + SELECT distinct + bank.code AS bank_code, + bank.abbreviation AS bank_name, + org.code AS org_code, + org.abbreviation AS org_name + FROM + sdi_xdata.fim_liab_compare bank, + sdi_xdata.fim_liab_compare org + WHERE + bank.LABEL = '银行' + AND org.LABEL = '主体' + ) +SELECT + dz.bank_code as bank_code + ,dz.bank_name as bank + -- ,dk.occupy_credit_quota_type + ,SUM(CASE WHEN dk.occupy_credit_quota_type <> '应付票据金额' THEN dk.this_amt ELSE 0 END) * (1/10000) AS loan_amt -- 贷款余额 + ,SUM(CASE WHEN dk.occupy_credit_quota_type = '应付票据金额' THEN dk.this_amt ELSE 0 END) * (1/10000) AS payable_amt -- 应付票据金额 + ,SUM(dk.this_amt) * (1/10000) as this_amt -- 合计 + ,(SUM(dk.this_amt) - SUM(dk.year_amt)) * (1/10000) AS year_add -- 较年初增长 + ,(SUM(dk.this_amt) - SUM(dk.last_amt)) * (1/10000) AS last_year_add +FROM + dz + left JOIN (select * from data_dk union all select * from data_yf ) AS dk ON dk.org_code = dz.org_code and dz.bank_code = dk.bank_code +where + dk.this_amt is not null or dk.year_amt is not null or dk.last_amt is not null +group by dz.bank_code,dz.bank_name + +-- ${switch(sData_type,'0',sStart_date,'1',sStart_mon,'2',sStart_year)}