From d7bf8b08ea598cc187de87b9a4b4a94984696e79 Mon Sep 17 00:00:00 2001 From: yangkunan Date: Wed, 27 Nov 2024 09:03:05 +0800 Subject: [PATCH] =?UTF-8?q?=E9=A6=96=E9=A1=B5=E6=8C=87=E6=A0=87=E4=BC=98?= =?UTF-8?q?=E5=8C=96=E8=B0=83=E6=95=B4+=E8=B4=A2=E5=8A=A1=E9=A2=84?= =?UTF-8?q?=E7=AE=97=E8=B4=B9=E7=94=A8=E6=89=8B=E5=B7=A5=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/sqldialects.xml | 1 + FineReport/tianbao/财务预算填报.sql | 51 ++ .../报表/费用/1-财务销售费用汇总表.sql | 42 +- .../财务主题/报表/费用/1_1_1OA表单下钻.sql | 23 +- .../报表/费用/2-财务管理费用汇总表.sql | 34 +- .../财务主题/报表/费用/3-研发费用汇总表.sql | 34 +- .../财务主题/报表/费用/4-财务费用汇总表.sql | 27 +- FineReport/销售主题/报表/终端奖励对账.sql | 3 +- .../销售主题/报表/销售业绩/酱酒计划执行表.sql | 3 + FineReport/销售主题/报表/首页.sql | 790 ++++++++++++++++-- 10 files changed, 926 insertions(+), 82 deletions(-) diff --git a/.idea/sqldialects.xml b/.idea/sqldialects.xml index 10c5002..3a165b1 100644 --- a/.idea/sqldialects.xml +++ b/.idea/sqldialects.xml @@ -17,6 +17,7 @@ + \ No newline at end of file diff --git a/FineReport/tianbao/财务预算填报.sql b/FineReport/tianbao/财务预算填报.sql index 24be4e5..263e262 100644 --- a/FineReport/tianbao/财务预算填报.sql +++ b/FineReport/tianbao/财务预算填报.sql @@ -74,3 +74,54 @@ FROM dwr.dim_subject WHERE (subject_code like '6601%' or subject_code like '6602%' or subject_code like '6603%' or subject_code like '6604%' ) + + + + + + + +DROP TABLE IF EXISTS sdi_oth.ods_finance_budget_data; +CREATE TABLE sdi_oth.ods_finance_budget_data ( + corp_name VARCHAR(100) + ,years VARCHAR(50) + ,fee_type VARCHAR(100) + ,subject_name VARCHAR(100) + ,amt VARCHAR(50) +); + + + + +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 +"')", "")} +) + SELECT + + a.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 diff --git a/FineReport/财务主题/报表/费用/1-财务销售费用汇总表.sql b/FineReport/财务主题/报表/费用/1-财务销售费用汇总表.sql index 3512d49..1e30146 100644 --- a/FineReport/财务主题/报表/费用/1-财务销售费用汇总表.sql +++ b/FineReport/财务主题/报表/费用/1-财务销售费用汇总表.sql @@ -351,16 +351,48 @@ where FROM base_info_last WHERE subject_code like '6601%' - ) +) +,tmp_data AS ( + SELECT + replace(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 replace(a.subject_name,':',': ') +) SELECT - subject_name AS subject_name -- 科目名称 + 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 -- 借方金额_上年累计 -FROM subject_result + ,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 - subject_name,subject_code + a.subject_name,subject_code ORDER BY - DECODE(subject_name,'广宣费','a','其中: 广告费','b',' 业务宣传费','c','职工薪酬','d','差旅费','e','折旧费','f','电商运营费','g','房租费','h','会议费','i','业务招待费','j','仓储费','k','通讯费','l','其他','m','合计','n') \ No newline at end of file + DECODE(a.subject_name,'广宣费','a','其中: 广告费','b',' 业务宣传费','c','职工薪酬','d','差旅费','e','折旧费','f','电商运营费','g','房租费','h','会议费','i','业务招待费','j','仓储费','k','通讯费','l','其他','m','合计','n') + + + + + diff --git a/FineReport/财务主题/报表/费用/1_1_1OA表单下钻.sql b/FineReport/财务主题/报表/费用/1_1_1OA表单下钻.sql index 6d8fdd0..1038dab 100644 --- a/FineReport/财务主题/报表/费用/1_1_1OA表单下钻.sql +++ b/FineReport/财务主题/报表/费用/1_1_1OA表单下钻.sql @@ -44,10 +44,25 @@ SELECT ,remark -- 备注 ,budget_type -- 预算类型 ,fund_content -- 支出内容 -FROM dwi.dwi_fim_oa_payment -WHERE corp_code in(SELECT corp_code FROM corp) -AND bill_date >= CONCAT('${sStart_mon}','-01') -AND bill_date <= CONCAT('${sEnd_mon}','-30') +FROM dwi.dwi_fim_oa_payment a +JOIN +( + SELECT + src_bill_codes + FROM dwi.dwi_fim_voucher + WHERE 1=1 + ${if(len(sPk)=0,"","AND pk = '" + sPk +"'")} + GROUP BY src_bill_codes +)b +ON a.bill_code = b.src_bill_codes +WHERE a.corp_code in(SELECT corp_code FROM corp) +AND a.bill_date >= CONCAT('${sStart_mon}','-01') +AND a.bill_date <= CONCAT('${sEnd_mon}','-30') + + + + + ${if(len(sBill)=0,"","AND bill_code IN ('" + sBill +"')")} diff --git a/FineReport/财务主题/报表/费用/2-财务管理费用汇总表.sql b/FineReport/财务主题/报表/费用/2-财务管理费用汇总表.sql index 2d1c462..752e36a 100644 --- a/FineReport/财务主题/报表/费用/2-财务管理费用汇总表.sql +++ b/FineReport/财务主题/报表/费用/2-财务管理费用汇总表.sql @@ -419,16 +419,40 @@ where 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 - subject_name AS subject_name -- 科目名称 + 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 -- 借方金额_上年累计 -FROM subject_result + ,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 - subject_name,subject_code + a.subject_name,subject_code ORDER BY - DECODE(subject_name,'职工薪酬','a','折旧费','b','物料消耗','c','存货损耗','d','办公费','e','咨询服务费','f','无形资产摊销','g','差旅费','h','物业管理费','i','水电气费','j','车辆使用费','k','业务招待费','l','使用权资产摊销','m','租赁费','n','通讯费','o','会议费','p','其他','q') \ No newline at end of file + DECODE(a.subject_name,'职工薪酬','a','折旧费','b','物料消耗','c','存货损耗','d','办公费','e','咨询服务费','f','无形资产摊销','g','差旅费','h','物业管理费','i','水电气费','j','车辆使用费','k','业务招待费','l','使用权资产摊销','m','租赁费','n','通讯费','o','会议费','p','其他','q') \ No newline at end of file diff --git a/FineReport/财务主题/报表/费用/3-研发费用汇总表.sql b/FineReport/财务主题/报表/费用/3-研发费用汇总表.sql index f85dc29..d604987 100644 --- a/FineReport/财务主题/报表/费用/3-研发费用汇总表.sql +++ b/FineReport/财务主题/报表/费用/3-研发费用汇总表.sql @@ -174,16 +174,40 @@ where AND subject_code NOT like '660409%' AND subject_code NOT like '660408%' AND subject_code NOT like '660402%') - ) + ),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 - subject_name AS subject_name -- 科目名称 + 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 -- 借方金额_上年累计 -FROM subject_result + ,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 - subject_name,subject_code + a.subject_name,subject_code ORDER BY - DECODE(subject_name,'材料费','a','职工薪酬','b','差旅费','c','加工费','d','技术服务费','e','其他','f') \ No newline at end of file + DECODE(a.subject_name,'材料费','a','职工薪酬','b','差旅费','c','加工费','d','技术服务费','e','其他','f') \ No newline at end of file diff --git a/FineReport/财务主题/报表/费用/4-财务费用汇总表.sql b/FineReport/财务主题/报表/费用/4-财务费用汇总表.sql index 0a74289..1e1c297 100644 --- a/FineReport/财务主题/报表/费用/4-财务费用汇总表.sql +++ b/FineReport/财务主题/报表/费用/4-财务费用汇总表.sql @@ -167,14 +167,32 @@ where -- -- FROM base_info_last -- WHERE subject_code like '6603%' +),tmp_data AS ( + SELECT + replace(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 replace(a.subject_name,'利息收入','减:利息收入') + UNION ALL + SELECT + + FROM sdi_oth.ods_finance_budget_data a + JOIN corp b + on a.corp_name = b.corp_name + ) SELECT - subject_name + t1.subject_name ,subject_code ,debit_amt_m -- 借方金额_当月累计 ,debit_amt_y -- 借方金额_当年累计 ,debit_amt_last_m -- 借方金额_上年同期 ,debit_amt_last_y -- 借方金额_上年累计 +,IFNULL(t2.amt,0) AS debit_amt_yusuan FROM ( SELECT @@ -198,10 +216,11 @@ SELECT ,IFNULL(SUM(CASE WHEN subject_name = '减:利息收入' THEN -1 * debit_amt_last_m ELSE debit_amt_last_m END ) / 10000,0) AS debit_amt_last_m -- 借方金额_上年同期 ,IFNULL(SUM(CASE WHEN subject_name = '减:利息收入' THEN -1 * debit_amt_last_y ELSE debit_amt_last_y END) / 10000,0) AS debit_amt_last_y -- 借方金额_上年累计 FROM subject_result -)t - +)t1 +LEFT JOIN tmp_data t2 +ON t1.subject_name = t2.subject_name ORDER BY - DECODE(subject_name,'利息支出','a','减:利息收入','b','汇兑损益','c','金融机构手续费','d','其他','e') + DECODE(t1.subject_name,'利息支出','a','减:利息收入','b','汇兑损益','c','金融机构手续费','d','其他','e') diff --git a/FineReport/销售主题/报表/终端奖励对账.sql b/FineReport/销售主题/报表/终端奖励对账.sql index 02bca7b..538f4b8 100644 --- a/FineReport/销售主题/报表/终端奖励对账.sql +++ b/FineReport/销售主题/报表/终端奖励对账.sql @@ -181,8 +181,7 @@ ${if(len(sDeal) == 0,"",if(REGEXP(sDeal,"^[0-9]+$"),"and deal_code = '"+sDeal+"' ${if(len(sIndi_type) == 0,"","and indi_type IN ('"+ sIndi_type +"')")} ORDER BY nvl(st.sorted_no,99) -limit ${pPage_rows} offset (${pJump_page}-1)*${pPage_rows} - +limit ${sPage_rows} offset (${pJump_page}-1)*${sPage_rows} diff --git a/FineReport/销售主题/报表/销售业绩/酱酒计划执行表.sql b/FineReport/销售主题/报表/销售业绩/酱酒计划执行表.sql index 7af2278..8a5872f 100644 --- a/FineReport/销售主题/报表/销售业绩/酱酒计划执行表.sql +++ b/FineReport/销售主题/报表/销售业绩/酱酒计划执行表.sql @@ -164,6 +164,9 @@ ${if(len(sBu) == 0,"","and stp.bu_code IN ('"+sBu+"')")} -- 经销商是否终止 ${if(len(sIs_stop_deal) == 0,"","and nvl(stp.is_suspend,'0') = '"+sIs_stop_deal+"'")} ORDER BY nvl(st.sorted_no,99) + +limit ${sPage_rows} offset (${pJump_page}-1)*${sPage_rows} + ; diff --git a/FineReport/销售主题/报表/首页.sql b/FineReport/销售主题/报表/首页.sql index 3b069cb..bb813fc 100644 --- a/FineReport/销售主题/报表/首页.sql +++ b/FineReport/销售主题/报表/首页.sql @@ -79,21 +79,109 @@ select '当期' AS time_type ,sum(ct_amt)/10000 AS amt from dm.v_ag_sal_contract a -where 1=1 -${IF(sPeriod=1,"AND ct_year ='"+ FORMAT(TODAY(),'yyyy') +"'","")} -- 年 -${IF(sPeriod=2,"AND CONCAT(to_char(signed_date,'yyyy'),EXTRACT(QUARTER FROM signed_date)) ='"+ CONCATENATE(left(today(),4),roundup(month(today())/3)) +"'","")} -- 季 -${IF(sPeriod=3,"AND to_char(signed_date,'yyyy-MM') ='"+ FORMAT(TODAY(),'yyyy-MM') +"'","")} -- 月 -${IF(sPeriod=4,"AND to_char(signed_date,'yyyy-MM-dd') = to_char(CURRENT_DATE - INTERVAL '1 day','yyyy-MM-dd') ","")} -- 日 +-- 权限控制 +where 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.bu_code -- 事业部 + OR per_code = a.region_code -- 大区 + OR per_code = a.office_code -- 办事处 +-- OR per_code = a.city_unit_code -- 城市单元 + ) + ) +${IF(len(sStart_date) > 0,"AND ct_year ='"+ FORMAT(sStart_date,'yyyy') +"'","")} +-- 年 +-- ${IF(sPeriod=1,"AND ct_year ='"+ FORMAT(sStart_date,'yyyy') +"'","")} +-- 季 +-- ${IF(sPeriod=2,"AND to_char(signed_date,'yyyy-MM-dd') >='"+ sStart_date +"'","")} +-- ${IF(sPeriod=2,"AND to_char(signed_date,'yyyy-MM-dd') <='"+ sEnd_date +"'","")} +-- 月 +-- ${IF(sPeriod=3,"AND to_char(signed_date,'yyyy-MM-dd') >='"+ sStart_date +"'","")} +--- ${IF(sPeriod=3,"AND to_char(signed_date,'yyyy-MM-dd') <='"+ sEnd_date +"'","")} +-- 日 +-- ${IF(sPeriod=4,"AND to_char(signed_date,'yyyy-MM-dd') >='"+ sStart_date +"'","")} +-- ${IF(sPeriod=4,"AND to_char(signed_date,'yyyy-MM-dd') <='"+ sEnd_date +"'","")} UNION ALL select '同期' AS time_type ,sum(ct_amt)/10000 AS amt from dm.v_ag_sal_contract a -WHERE 1=1 -${IF(sPeriod=1,"AND ct_year = EXTRACT(YEAR FROM CURRENT_DATE - INTERVAL '1 year') ","")} -- 去年 -${IF(sPeriod=2,"AND CONCAT(to_char(signed_date,'yyyy'),EXTRACT(QUARTER FROM signed_date)) = CONCAT(EXTRACT(YEAR FROM CURRENT_DATE - INTERVAL '1 year'),EXTRACT(QUARTER FROM CURRENT_DATE)) ","")} -- 去年同季 -${IF(sPeriod=3,"AND to_char(signed_date,'yyyy-MM') = to_char(CURRENT_DATE - INTERVAL '12 month','yyyy-MM') ","")} -- 去年同月 -${IF(sPeriod=4,"AND to_char(signed_date,'yyyy-MM-dd') = to_char((CURRENT_DATE - INTERVAL '1 day') - INTERVAL '1 year','yyyy-MM-dd') ","")} -- 当前时间的前天 +-- 权限控制 +where 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.bu_code -- 事业部 + OR per_code = a.region_code -- 大区 + OR per_code = a.office_code -- 办事处 + OR per_code = a.city_unit_code -- 城市单元 + ) + ) + + +${IF(len(sStart_date) > 0,"AND ct_year = '" + left(yeardelta(sStart_date,-1),4) + "'","")} +-- 去年 +-- ${IF(sPeriod=1,"AND ct_year = '" + left(yeardelta(sStart_date,-1),4) + "'","")} +-- 去年同季 +-- ${IF(sPeriod=2,"AND to_char(signed_date,'yyyy-MM-dd') >='"+ yeardelta(sStart_date,-1) +"'","")} +-- ${IF(sPeriod=2,"AND to_char(signed_date,'yyyy-MM-dd') <='"+ yeardelta(sEnd_date,-1) +"'","")} +-- 去年同月 +-- ${IF(sPeriod=3,"AND to_char(signed_date,'yyyy-MM-dd') >='"+ yeardelta(sStart_date,-1) +"'","")} +-- ${IF(sPeriod=3,"AND to_char(signed_date,'yyyy-MM-dd') <='"+ yeardelta(sEnd_date,-1) +"'","")} +-- 去年同日 +-- ${IF(sPeriod=4,"AND to_char(signed_date,'yyyy-MM-dd') >='"+ yeardelta(sStart_date,-1) +"'","")} +-- ${IF(sPeriod=4,"AND to_char(signed_date,'yyyy-MM-dd') <='"+ yeardelta(sEnd_date,-1) +"'","")} + +UNION ALL +select + '当期回款' time_type + ,sum(amt_bu) AS amt +from dm.v_sal_indicator_bu a +where (indi_type='回款' OR indi_type='电商订单') +-- 权限控制 + 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.bu_code -- 事业部 +-- OR per_code = a.region_code -- 大区 +-- OR per_code = a.office_code -- 办事处 +-- OR per_code = a.city_unit_code -- 城市单元 + ) + ) + + ${IF(len(sStart_date) > 0,"AND to_char(stat_date,'yyyy') ='"+ FORMAT(sStart_date,'yyyy') +"'","")} +-- 年 + -- ${IF(sPeriod=1,"AND to_char(stat_date,'yyyy') ='"+ FORMAT(sStart_date,'yyyy') +"'","")} +-- 季 + -- ${IF(sPeriod=2,"AND to_char(stat_date,'yyyy-MM-dd') >='"+ sStart_date +"'","")} +-- ${IF(sPeriod=2,"AND to_char(stat_date,'yyyy-MM-dd') <='"+ sEnd_date +"'","")} +-- 月 +-- ${IF(sPeriod=3,"AND to_char(stat_date,'yyyy-MM-dd') >='"+ sStart_date +"'","")} +-- ${IF(sPeriod=3,"AND to_char(stat_date,'yyyy-MM-dd') <='"+ sEnd_date +"'","")} +-- 日 +-- ${IF(sPeriod=4,"AND to_char(stat_date,'yyyy-MM-dd') >='"+ sStart_date +"'","")} +-- ${IF(sPeriod=4,"AND to_char(stat_date,'yyyy-MM-dd') <='"+ sEnd_date +"'","")} + -- ====================================== @@ -103,39 +191,108 @@ ${IF(sPeriod=4,"AND to_char(signed_date,'yyyy-MM-dd') = to_char((CURRENT_DATE - select '当期' time_type ,sum(amt_bu) AS amt -from dm.v_sal_indicator_bu +from dm.v_sal_indicator_bu a where (indi_type='回款' OR indi_type='电商订单') -${IF(sPeriod=1,"AND to_char(stat_date,'yyyy') ='"+ FORMAT(TODAY(),'yyyy') +"'","")} -- 年 -${IF(sPeriod=2,"AND CONCAT(to_char(stat_date,'yyyy'),EXTRACT(QUARTER FROM stat_date)) ='"+ CONCATENATE(left(today(),4),roundup(month(today())/3)) +"'","")} -- 季 -${IF(sPeriod=3,"AND to_char(stat_date,'yyyy-MM') ='"+ FORMAT(TODAY(),'yyyy-MM') +"'","")} -- 月 -${IF(sPeriod=4,"AND to_char(stat_date,'yyyy-MM-dd') = to_char(CURRENT_DATE - INTERVAL '1 day','yyyy-MM-dd') ","")} -- 日 +-- 权限控制 +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.bu_code -- 事业部 +-- OR per_code = a.region_code -- 大区 +-- OR per_code = a.office_code -- 办事处 +-- OR per_code = a.city_unit_code -- 城市单元 + ) + ) +-- 年 +${IF(sPeriod=1,"AND to_char(stat_date,'yyyy') ='"+ FORMAT(sStart_date,'yyyy') +"'","")} +-- 季 +${IF(sPeriod=2,"AND to_char(stat_date,'yyyy-MM-dd') >='"+ sStart_date +"'","")} +${IF(sPeriod=2,"AND to_char(stat_date,'yyyy-MM-dd') <='"+ sEnd_date +"'","")} +-- 月 +${IF(sPeriod=3,"AND to_char(stat_date,'yyyy-MM-dd') >='"+ sStart_date +"'","")} +${IF(sPeriod=3,"AND to_char(stat_date,'yyyy-MM-dd') <='"+ sEnd_date +"'","")} +-- 日 +${IF(sPeriod=4,"AND to_char(stat_date,'yyyy-MM-dd') >='"+ sStart_date +"'","")} +${IF(sPeriod=4,"AND to_char(stat_date,'yyyy-MM-dd') <='"+ sEnd_date +"'","")} + UNION ALL select '同期' time_type ,sum(amt_bu) AS amt -from dm.v_sal_indicator_bu +from dm.v_sal_indicator_bu a where (indi_type='回款' OR indi_type='电商订单') -${IF(sPeriod=1,"AND to_char(stat_date,'yyyy') = EXTRACT(YEAR FROM CURRENT_DATE - INTERVAL '1 year') ","")} -- 去年 -${IF(sPeriod=2,"AND CONCAT(to_char(stat_date,'yyyy'),EXTRACT(QUARTER FROM stat_date)) = CONCAT(EXTRACT(YEAR FROM CURRENT_DATE - INTERVAL '1 year'),EXTRACT(QUARTER FROM CURRENT_DATE)) ","")} -- 去年同季 -${IF(sPeriod=3,"AND to_char(stat_date,'yyyy-MM') = to_char(CURRENT_DATE - INTERVAL '12 month','yyyy-MM') ","")} -- 去年同月 -${IF(sPeriod=4,"AND to_char(stat_date,'yyyy-MM-dd') = to_char((CURRENT_DATE - INTERVAL '1 day') - INTERVAL '1 year','yyyy-MM-dd') ","")} -- 当前时间的前天 + 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.bu_code -- 事业部 +-- OR per_code = a.region_code -- 大区 +-- OR per_code = a.office_code -- 办事处 +-- OR per_code = a.city_unit_code -- 城市单元 + ) + ) +-- 去年 +${IF(sPeriod=1,"AND to_char(stat_date,'yyyy') = '" + left(yeardelta(sStart_date,-1),4) + "'","")} +-- 去年同季 +${IF(sPeriod=2,"AND to_char(stat_date,'yyyy-MM-dd') >='"+ yeardelta(sStart_date,-1) +"'","")} +${IF(sPeriod=2,"AND to_char(stat_date,'yyyy-MM-dd') <='"+ yeardelta(sEnd_date,-1) +"'","")} +-- 去年同月 +${IF(sPeriod=3,"AND to_char(stat_date,'yyyy-MM-dd') >='"+ yeardelta(sStart_date,-1) +"'","")} +${IF(sPeriod=3,"AND to_char(stat_date,'yyyy-MM-dd') <='"+ yeardelta(sEnd_date,-1) +"'","")} +-- 去年同日 +${IF(sPeriod=4,"AND to_char(stat_date,'yyyy-MM-dd') >='"+ yeardelta(sStart_date,-1) +"'","")} +${IF(sPeriod=4,"AND to_char(stat_date,'yyyy-MM-dd') <='"+ yeardelta(sEnd_date,-1) +"'","")} UNION select '目标' time_type, sum(amt_bu) AS amt -from dm.v_sal_indicator_bu +from dm.v_sal_indicator_bu a where ${switch(sPeriod ,1,"indi_type='年度目标_事业部_回款'" ,2,"indi_type='季度目标_事业部_回款'" ,3,"indi_type='月度目标_事业部_回款'" ,4,"indi_type='不存在'")} - - ${IF(sPeriod=1,"AND to_char(stat_date,'yyyy') ='"+ FORMAT(TODAY(),'yyyy') +"'","")} -- 年 - ${IF(sPeriod=2,"AND CONCAT(to_char(stat_date,'yyyy'),EXTRACT(QUARTER FROM stat_date)) ='"+ CONCATENATE(left(today(),4),roundup(month(today())/3)) +"'","")} -- 季 - ${IF(sPeriod=3,"AND to_char(stat_date,'yyyy-MM') ='"+ FORMAT(TODAY(),'yyyy-MM') +"'","")} -- 月 - ${IF(sPeriod=4,"AND to_char(stat_date,'yyyy-MM-dd') = to_char(CURRENT_DATE - INTERVAL '1 day','yyyy-MM-dd') ","")} -- 日 - +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.bu_code -- 事业部 +-- OR per_code = a.region_code -- 大区 +-- OR per_code = a.office_code -- 办事处 +-- OR per_code = a.city_unit_code -- 城市单元 + ) + ) +-- 年 +${IF(sPeriod=1,"AND to_char(stat_date,'yyyy') ='"+ FORMAT(sStart_date,'yyyy') +"'","")} +-- 季 +${IF(sPeriod=2,"AND to_char(stat_date,'yyyy-MM-dd') >='"+ sStart_date +"'","")} +${IF(sPeriod=2,"AND to_char(stat_date,'yyyy-MM-dd') <='"+ sEnd_date +"'","")} +-- 月 +${IF(sPeriod=3,"AND to_char(stat_date,'yyyy-MM-dd') >='"+ sStart_date +"'","")} +${IF(sPeriod=3,"AND to_char(stat_date,'yyyy-MM-dd') <='"+ sEnd_date +"'","")} +-- 日 +${IF(sPeriod=4,"AND to_char(stat_date,'yyyy-MM-dd') >='"+ sStart_date +"'","")} +${IF(sPeriod=4,"AND to_char(stat_date,'yyyy-MM-dd') <='"+ sEnd_date +"'","")} -- ====================================== -- 经销商出货 @@ -144,37 +301,111 @@ where ${switch(sPeriod select '当期' time_type ,sum(amt_bu) AS amt -from dm.v_sal_indicator_bu +from dm.v_sal_indicator_bu a where (indi_type='出货' OR indi_type='电商订单') -${IF(sPeriod=1,"AND to_char(stat_date,'yyyy') ='"+ FORMAT(TODAY(),'yyyy') +"'","")} -- 年 -${IF(sPeriod=2,"AND CONCAT(to_char(stat_date,'yyyy'),EXTRACT(QUARTER FROM stat_date)) ='"+ CONCATENATE(left(today(),4),roundup(month(today())/3)) +"'","")} -- 季 -${IF(sPeriod=3,"AND to_char(stat_date,'yyyy-MM') ='"+ FORMAT(TODAY(),'yyyy-MM') +"'","")} -- 月 -${IF(sPeriod=4,"AND to_char(stat_date,'yyyy-MM-dd') = to_char(CURRENT_DATE - INTERVAL '1 day','yyyy-MM-dd') ","")} -- 日 +-- 权限控制 + 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.bu_code -- 事业部 +-- OR per_code = a.region_code -- 大区 +-- OR per_code = a.office_code -- 办事处 +-- OR per_code = a.city_unit_code -- 城市单元 + ) + ) +-- 年 + ${IF(sPeriod=1,"AND to_char(stat_date,'yyyy') ='"+ FORMAT(sStart_date,'yyyy') +"'","")} +-- 季 + ${IF(sPeriod=2,"AND to_char(stat_date,'yyyy-MM-dd') >='"+ sStart_date +"'","")} + ${IF(sPeriod=2,"AND to_char(stat_date,'yyyy-MM-dd') <='"+ sEnd_date +"'","")} +-- 月 + ${IF(sPeriod=3,"AND to_char(stat_date,'yyyy-MM-dd') >='"+ sStart_date +"'","")} + ${IF(sPeriod=3,"AND to_char(stat_date,'yyyy-MM-dd') <='"+ sEnd_date +"'","")} +-- 日 + ${IF(sPeriod=4,"AND to_char(stat_date,'yyyy-MM-dd') >='"+ sStart_date +"'","")} + ${IF(sPeriod=4,"AND to_char(stat_date,'yyyy-MM-dd') <='"+ sEnd_date +"'","")} + UNION ALL select '同期' time_type ,sum(amt_bu) AS amt -from dm.v_sal_indicator_bu +from dm.v_sal_indicator_bu a where (indi_type='出货' OR indi_type='电商订单') -${IF(sPeriod=1,"AND to_char(stat_date,'yyyy') = EXTRACT(YEAR FROM CURRENT_DATE - INTERVAL '1 year') ","")} -- 去年 -${IF(sPeriod=2,"AND CONCAT(to_char(stat_date,'yyyy'),EXTRACT(QUARTER FROM stat_date)) = CONCAT(EXTRACT(YEAR FROM CURRENT_DATE - INTERVAL '1 year'),EXTRACT(QUARTER FROM CURRENT_DATE)) ","")} -- 去年同季 -${IF(sPeriod=3,"AND to_char(stat_date,'yyyy-MM') = to_char(CURRENT_DATE - INTERVAL '12 month','yyyy-MM') ","")} -- 去年同月 -${IF(sPeriod=4,"AND to_char(stat_date,'yyyy-MM-dd') = to_char((CURRENT_DATE - INTERVAL '1 day') - INTERVAL '1 year','yyyy-MM-dd') ","")} -- 当前时间的前天 + 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.bu_code -- 事业部 +-- OR per_code = a.region_code -- 大区 +-- OR per_code = a.office_code -- 办事处 +-- OR per_code = a.city_unit_code -- 城市单元 + ) + ) + +-- 去年 + ${IF(sPeriod=1,"AND to_char(stat_date,'yyyy') = '" + left(yeardelta(sStart_date,-1),4) + "'","")} +-- 去年同季 + ${IF(sPeriod=2,"AND to_char(stat_date,'yyyy-MM-dd') >='"+ yeardelta(sStart_date,-1) +"'","")} + ${IF(sPeriod=2,"AND to_char(stat_date,'yyyy-MM-dd') <='"+ yeardelta(sEnd_date,-1) +"'","")} +-- 去年同月 + ${IF(sPeriod=3,"AND to_char(stat_date,'yyyy-MM-dd') >='"+ yeardelta(sStart_date,-1) +"'","")} + ${IF(sPeriod=3,"AND to_char(stat_date,'yyyy-MM-dd') <='"+ yeardelta(sEnd_date,-1) +"'","")} +-- 去年同日 + ${IF(sPeriod=4,"AND to_char(stat_date,'yyyy-MM-dd') >='"+ yeardelta(sStart_date,-1) +"'","")} + ${IF(sPeriod=4,"AND to_char(stat_date,'yyyy-MM-dd') <='"+ yeardelta(sEnd_date,-1) +"'","")} + + UNION select '目标' time_type ,sum(amt_bu) AS amt -from dm.v_sal_indicator_bu +from dm.v_sal_indicator_bu a where ${switch(sPeriod ,1,"indi_type='年度目标_事业部_出货'" ,2,"indi_type='季度目标_事业部_出货'" ,3,"indi_type='月度目标_事业部_出货'" ,4,"indi_type='不存在'")} -${IF(sPeriod=1,"AND to_char(stat_date,'yyyy') ='"+ FORMAT(TODAY(),'yyyy') +"'","")} -- 年 -${IF(sPeriod=2,"AND CONCAT(to_char(stat_date,'yyyy'),EXTRACT(QUARTER FROM stat_date)) ='"+ CONCATENATE(left(today(),4),roundup(month(today())/3)) +"'","")} -- 季 -${IF(sPeriod=3,"AND to_char(stat_date,'yyyy-MM') ='"+ FORMAT(TODAY(),'yyyy-MM') +"'","")} -- 月 -${IF(sPeriod=4,"AND to_char(stat_date,'yyyy-MM-dd') = to_char(CURRENT_DATE - INTERVAL '1 day','yyyy-MM-dd') ","")} -- 日 + 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.bu_code -- 事业部 +-- OR per_code = a.region_code -- 大区 +-- OR per_code = a.office_code -- 办事处 +-- OR per_code = a.city_unit_code -- 城市单元 + ) + ) +-- 年 + ${IF(sPeriod=1,"AND to_char(stat_date,'yyyy') ='"+ FORMAT(sStart_date,'yyyy') +"'","")} +-- 季 + ${IF(sPeriod=2,"AND to_char(stat_date,'yyyy-MM-dd') >='"+ sStart_date +"'","")} + ${IF(sPeriod=2,"AND to_char(stat_date,'yyyy-MM-dd') <='"+ sEnd_date +"'","")} +-- 月 + ${IF(sPeriod=3,"AND to_char(stat_date,'yyyy-MM-dd') >='"+ sStart_date +"'","")} + ${IF(sPeriod=3,"AND to_char(stat_date,'yyyy-MM-dd') <='"+ sEnd_date +"'","")} +-- 日 + ${IF(sPeriod=4,"AND to_char(stat_date,'yyyy-MM-dd') >='"+ sStart_date +"'","")} + ${IF(sPeriod=4,"AND to_char(stat_date,'yyyy-MM-dd') <='"+ sEnd_date +"'","")} -- ====================================== @@ -184,33 +415,478 @@ ${IF(sPeriod=4,"AND to_char(stat_date,'yyyy-MM-dd') = to_char(CURRENT_DATE - IN select '当期' time_type ,sum(amt_bu) AS amt -from dm.v_sal_indicator_bu +from dm.v_sal_indicator_bu a where (indi_type='发货' OR indi_type='费用发货') -${IF(sPeriod=1,"AND to_char(stat_date,'yyyy') ='"+ FORMAT(TODAY(),'yyyy') +"'","")} -- 年 -${IF(sPeriod=2,"AND CONCAT(to_char(stat_date,'yyyy'),EXTRACT(QUARTER FROM stat_date)) ='"+ CONCATENATE(left(today(),4),roundup(month(today())/3)) +"'","")} -- 季 -${IF(sPeriod=3,"AND to_char(stat_date,'yyyy-MM') ='"+ FORMAT(TODAY(),'yyyy-MM') +"'","")} -- 月 -${IF(sPeriod=4,"AND to_char(stat_date,'yyyy-MM-dd') = to_char(CURRENT_DATE - INTERVAL '1 day','yyyy-MM-dd') ","")} -- 日 +-- 权限控制 + 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.bu_code -- 事业部 +-- OR per_code = a.region_code -- 大区 +-- OR per_code = a.office_code -- 办事处 +-- OR per_code = a.city_unit_code -- 城市单元 + ) + ) +-- 年 + ${IF(sPeriod=1,"AND to_char(stat_date,'yyyy') ='"+ FORMAT(sStart_date,'yyyy') +"'","")} +-- 季 + ${IF(sPeriod=2,"AND to_char(stat_date,'yyyy-MM-dd') >='"+ sStart_date +"'","")} + ${IF(sPeriod=2,"AND to_char(stat_date,'yyyy-MM-dd') <='"+ sEnd_date +"'","")} +-- 月 + ${IF(sPeriod=3,"AND to_char(stat_date,'yyyy-MM-dd') >='"+ sStart_date +"'","")} + ${IF(sPeriod=3,"AND to_char(stat_date,'yyyy-MM-dd') <='"+ sEnd_date +"'","")} +-- 日 + ${IF(sPeriod=4,"AND to_char(stat_date,'yyyy-MM-dd') >='"+ sStart_date +"'","")} + ${IF(sPeriod=4,"AND to_char(stat_date,'yyyy-MM-dd') <='"+ sEnd_date +"'","")} UNION ALL select '同期' time_type ,sum(amt_bu) AS amt -from dm.v_sal_indicator_bu +from dm.v_sal_indicator_bu a where (indi_type='发货' OR indi_type='费用发货') -${IF(sPeriod=1,"AND to_char(stat_date,'yyyy') = EXTRACT(YEAR FROM CURRENT_DATE - INTERVAL '1 year') ","")} -- 去年 -${IF(sPeriod=2,"AND CONCAT(to_char(stat_date,'yyyy'),EXTRACT(QUARTER FROM stat_date)) = CONCAT(EXTRACT(YEAR FROM CURRENT_DATE - INTERVAL '1 year'),EXTRACT(QUARTER FROM CURRENT_DATE)) ","")} -- 去年同季 -${IF(sPeriod=3,"AND to_char(stat_date,'yyyy-MM') = to_char(CURRENT_DATE - INTERVAL '12 month','yyyy-MM') ","")} -- 去年同月 -${IF(sPeriod=4,"AND to_char(stat_date,'yyyy-MM-dd') = to_char((CURRENT_DATE - INTERVAL '1 day') - INTERVAL '1 year','yyyy-MM-dd') ","")} -- 当前时间的前天 + 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.bu_code -- 事业部 +-- OR per_code = a.region_code -- 大区 +-- OR per_code = a.office_code -- 办事处 +-- OR per_code = a.city_unit_code -- 城市单元 + ) + ) + +-- 去年 + ${IF(sPeriod=1,"AND to_char(stat_date,'yyyy') = '" + left(yeardelta(sStart_date,-1),4) + "'","")} +-- 去年同季 + ${IF(sPeriod=2,"AND to_char(stat_date,'yyyy-MM-dd') >='"+ yeardelta(sStart_date,-1) +"'","")} + ${IF(sPeriod=2,"AND to_char(stat_date,'yyyy-MM-dd') <='"+ yeardelta(sEnd_date,-1) +"'","")} +-- 去年同月 + ${IF(sPeriod=3,"AND to_char(stat_date,'yyyy-MM-dd') >='"+ yeardelta(sStart_date,-1) +"'","")} + ${IF(sPeriod=3,"AND to_char(stat_date,'yyyy-MM-dd') <='"+ yeardelta(sEnd_date,-1) +"'","")} +-- 去年同日 + ${IF(sPeriod=4,"AND to_char(stat_date,'yyyy-MM-dd') >='"+ yeardelta(sStart_date,-1) +"'","")} + ${IF(sPeriod=4,"AND to_char(stat_date,'yyyy-MM-dd') <='"+ yeardelta(sEnd_date,-1) +"'","")} + + UNION select '目标' time_type ,sum(amt_bu) AS amt -from dm.v_sal_indicator_bu +from dm.v_sal_indicator_bu a where ${switch(sPeriod ,1,"indi_type IN('年度目标_事业部_费用发货','年度目标_事业部_销售发货')" ,2,"indi_type IN('季度目标_事业部_费用发货','季度目标_事业部_销售发货')" ,3,"indi_type IN('月度目标_事业部_费用发货','月度目标_事业部_销售发货')" ,4,"indi_type IN('不存在')")} -${IF(sPeriod=1,"AND to_char(stat_date,'yyyy') ='"+ FORMAT(TODAY(),'yyyy') +"'","")} -- 年 -${IF(sPeriod=2,"AND CONCAT(to_char(stat_date,'yyyy'),EXTRACT(QUARTER FROM stat_date)) ='"+ CONCATENATE(left(today(),4),roundup(month(today())/3)) +"'","")} -- 季 -${IF(sPeriod=3,"AND to_char(stat_date,'yyyy-MM') ='"+ FORMAT(TODAY(),'yyyy-MM') +"'","")} -- 月 -${IF(sPeriod=4,"AND to_char(stat_date,'yyyy-MM-dd') = to_char(CURRENT_DATE - INTERVAL '1 day','yyyy-MM-dd') ","")} -- 日 + 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.bu_code -- 事业部 +-- OR per_code = a.region_code -- 大区 +-- OR per_code = a.office_code -- 办事处 +-- OR per_code = a.city_unit_code -- 城市单元 + ) + ) +-- 年 + ${IF(sPeriod=1,"AND to_char(stat_date,'yyyy') ='"+ FORMAT(sStart_date,'yyyy') +"'","")} +-- 季 + ${IF(sPeriod=2,"AND to_char(stat_date,'yyyy-MM-dd') >='"+ sStart_date +"'","")} + ${IF(sPeriod=2,"AND to_char(stat_date,'yyyy-MM-dd') <='"+ sEnd_date +"'","")} +-- 月 + ${IF(sPeriod=3,"AND to_char(stat_date,'yyyy-MM-dd') >='"+ sStart_date +"'","")} + ${IF(sPeriod=3,"AND to_char(stat_date,'yyyy-MM-dd') <='"+ sEnd_date +"'","")} +-- 日 + ${IF(sPeriod=4,"AND to_char(stat_date,'yyyy-MM-dd') >='"+ sStart_date +"'","")} + ${IF(sPeriod=4,"AND to_char(stat_date,'yyyy-MM-dd') <='"+ sEnd_date +"'","")} + + +-- ====================================== +-- 财务资金状况 +-- ====================================== + + + +-- 资金计划与执行 +select +'当期' AS time_type +,'收入' AS fee_type -- 收支方向 收入/支出 +,SUM(CASE WHEN trade_direction = '收入' THEN amt ELSE 0 END) / 10000 AS amt +from dm.dm_fim_fud_indicator a +where corp_code not in ('0101','0103','0102') +AND trade_direction = '收入' +AND indi_type = '执行' +AND plan_item_name NOT IN ('内部调拨收入','取得贷款') +AND owner_bank_account <> '22195101040012008' +-- 权限控制 +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 -- 事业部 +-- OR per_code = a.region_code -- 大区 +-- OR per_code = a.office_code -- 办事处 +-- OR per_code = a.city_unit_code -- 城市单元 + ) +) +-- 年 + ${IF(sPeriod_fin=1,"AND to_char(stat_date,'yyyy') ='"+ FORMAT(sStart_date_fin,'yyyy') +"'","")} +-- 月 + ${IF(sPeriod_fin=2,"AND to_char(stat_date,'yyyy-MM') = '"+ FORMAT(sStart_date_fin,'yyyy-MM') +"'","")} +-- 日 + ${IF(sPeriod_fin=3,"AND to_char(stat_date,'yyyy-MM-dd') >= '"+ sStart_date_fin +"'","")} + ${IF(sPeriod_fin=4,"AND to_char(stat_date,'yyyy-MM-dd') <= '"+ sStart_date_fin +"'","")} +UNION ALL +select +'当期' AS time_type +,'支出' AS fee_type -- 收支方向 收入/支出 +,SUM(CASE WHEN trade_direction = '支出' THEN amt ELSE 0 END) / 10000 AS amt +from dm.dm_fim_fud_indicator a +where corp_code not in ('0101','0103','0102') + AND trade_direction = '支出' + AND indi_type = '执行' + AND plan_item_name NOT IN ('内部调拨支出','偿还贷款') + AND owner_bank_account <> '22195101040012008' +-- 权限控制 + 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 -- 事业部 +-- OR per_code = a.region_code -- 大区 +-- OR per_code = a.office_code -- 办事处 +-- OR per_code = a.city_unit_code -- 城市单元 + ) +) + +-- 年 + ${IF(sPeriod_fin=1,"AND to_char(stat_date,'yyyy') ='"+ FORMAT(sStart_date_fin,'yyyy') +"'","")} +-- 月 + ${IF(sPeriod_fin=2,"AND to_char(stat_date,'yyyy-MM') ='"+ FORMAT(sStart_date_fin,'yyyy-MM') +"'","")} +-- 日 + ${IF(sPeriod_fin=3,"AND to_char(stat_date,'yyyy-MM-dd') >='"+ sStart_date_fin +"'","")} + ${IF(sPeriod_fin=4,"AND to_char(stat_date,'yyyy-MM-dd') <='"+ sStart_date_fin +"'","")} +UNION ALL +-- 月初资金余额要取上月的月初的,年初要取上年末12月份的 +select +'当期' AS time_type +,'余额' AS fee_type +,sum(amt) / 10000 AS AMT +from dm.dm_fim_fud_balance a +where corp_code not in ('0101','0103','0102') +AND owner_bank_account NOT IN ('20351059900100000512291' + ,'22195101040012008' + ,'信用证保证金' + ,'22195162750000012' + ,'22195162650000017' + ,'22195162350000014' + ,'22195162050000015' + ,'951018033000447470') +-- 权限控制 + 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 -- 事业部 +-- OR per_code = a.region_code -- 大区 +-- OR per_code = a.office_code -- 办事处 +-- OR per_code = a.city_unit_code -- 城市单元 + ) +) +-- 年 + ${IF(sPeriod_fin=1,"AND to_char(stat_date,'yyyy') ='"+ FORMAT(sStart_date_fin,'yyyy') +"'","")} +-- 月 + ${IF(sPeriod_fin=2,"AND to_char(stat_date,'yyyy-MM') ='"+ FORMAT(MONTHDELTA(sStart_date_fin,-1),'yyyy-MM') +"'","")} +-- 日 + ${IF(sPeriod_fin=3,"AND to_char(stat_date,'yyyy-MM-dd') >='"+ sStart_date_fin +"'","")} + ${IF(sPeriod_fin=4,"AND to_char(stat_date,'yyyy-MM-dd') <='"+ sStart_date_fin +"'","")} + +UNION ALL +-- 资金计划与执行 +select + '上期' AS time_type + ,'收入' AS fee_type -- 收支方向 收入/支出 + ,SUM(CASE WHEN trade_direction = '收入' THEN amt ELSE 0 END) / 10000 AS amt +from dm.dm_fim_fud_indicator a +where corp_code not in ('0101','0103','0102') + AND trade_direction = '收入' + AND indi_type = '执行' + AND plan_item_name NOT IN ('内部调拨收入','取得贷款') + AND owner_bank_account <> '22195101040012008' +-- 权限控制 + 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 -- 事业部 +-- OR per_code = a.region_code -- 大区 +-- OR per_code = a.office_code -- 办事处 +-- OR per_code = a.city_unit_code -- 城市单元 + ) +) +-- 上年 + ${IF(sPeriod_fin=1,"AND to_char(stat_date,'yyyy') ='"+ FORMAT(YEARDELTA(sStart_date_fin,-1),'yyyy') +"'","")} +-- 上月 + ${IF(sPeriod_fin=2,"AND to_char(stat_date,'yyyy-MM') = '"+ FORMAT(MONTHDELTA(sStart_date_fin,-1),'yyyy-MM') +"'","")} +-- 昨日 + ${IF(sPeriod_fin=3,"AND to_char(stat_date,'yyyy-MM-dd') >= '"+ DATEDELTA(sStart_date_fin,-1) +"'","")} + ${IF(sPeriod_fin=4,"AND to_char(stat_date,'yyyy-MM-dd') <= '"+ DATEDELTA(sStart_date_fin,-1) +"'","")} +UNION ALL +select + '上期' AS time_type + ,'支出' AS fee_type -- 收支方向 收入/支出 + ,SUM(CASE WHEN trade_direction = '支出' THEN amt ELSE 0 END) / 10000 AS amt +from dm.dm_fim_fud_indicator a +where corp_code not in ('0101','0103','0102') + AND trade_direction = '支出' + AND indi_type = '执行' + AND plan_item_name NOT IN ('内部调拨支出','偿还贷款') + AND owner_bank_account <> '22195101040012008' +-- 权限控制 + 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 -- 事业部 +-- OR per_code = a.region_code -- 大区 +-- OR per_code = a.office_code -- 办事处 +-- OR per_code = a.city_unit_code -- 城市单元 + ) +) + +-- 上年 + ${IF(sPeriod_fin=1,"AND to_char(stat_date,'yyyy') ='"+ FORMAT(YEARDELTA(sStart_date_fin,-1),'yyyy') +"'","")} +-- 上月 + ${IF(sPeriod_fin=2,"AND to_char(stat_date,'yyyy-MM') = '"+ FORMAT(MONTHDELTA(sStart_date_fin,-1),'yyyy-MM') +"'","")} +-- 昨日 + ${IF(sPeriod_fin=3,"AND to_char(stat_date,'yyyy-MM-dd') >= '"+ DATEDELTA(sStart_date_fin,-1) +"'","")} + ${IF(sPeriod_fin=4,"AND to_char(stat_date,'yyyy-MM-dd') <= '"+ DATEDELTA(sStart_date_fin,-1) +"'","")} +UNION ALL +-- 月初资金余额要取上月的月初的,年初要取上年末12月份的 +select + '上期' AS time_type + ,'余额' AS fee_type + ,sum(amt) / 10000 AS AMT +from dm.dm_fim_fud_balance a +where corp_code not in ('0101','0103','0102') + AND owner_bank_account NOT IN ('20351059900100000512291' + ,'22195101040012008' + ,'信用证保证金' + ,'22195162750000012' + ,'22195162650000017' + ,'22195162350000014' + ,'22195162050000015' + ,'951018033000447470') +-- 权限控制 + 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 -- 事业部 +-- OR per_code = a.region_code -- 大区 +-- OR per_code = a.office_code -- 办事处 +-- OR per_code = a.city_unit_code -- 城市单元 + ) +) +-- 上年 + ${IF(sPeriod_fin=1,"AND to_char(stat_date,'yyyy') ='"+ FORMAT(YEARDELTA(sStart_date_fin,-1),'yyyy') +"'","")} +-- 上月 + ${IF(sPeriod_fin=2,"AND to_char(stat_date,'yyyy-MM') ='"+ FORMAT(MONTHDELTA(sStart_date_fin,-2),'yyyy-MM') +"'","")} +-- 昨日 + ${IF(sPeriod_fin=3,"AND to_char(stat_date,'yyyy-MM-dd') >= '"+ DATEDELTA(sStart_date_fin,-1) +"'","")} + ${IF(sPeriod_fin=4,"AND to_char(stat_date,'yyyy-MM-dd') <= '"+ DATEDELTA(sStart_date_fin,-1) +"'","")} + + + +-- ====================================== +-- 财务资金月度 +-- ====================================== + + +; +-- 资金计划与执行 + +SELECT +t.stat_date AS stat_date +,to_char(t.stat_date,'MM') as month +,CASE WHEN t.fee_type = '收入' THEN amt ELSE 0 END AS revenue_fee -- 收入 +,CASE WHEN t.fee_type = '支出' THEN amt ELSE 0 END AS expend_fee -- 支出 +,CASE WHEN t.fee_type = '余额' THEN amt ELSE 0 END AS balance_fee -- 余额 + +FROM +( + select + stat_date + ,'收入' AS fee_type -- 收支方向 收入/支出 + ,SUM(CASE WHEN trade_direction = '收入' THEN amt ELSE 0 END) / 10000 AS amt + from dm.dm_fim_fud_indicator a + where corp_code not in ('0101','0103','0102') + AND trade_direction = '收入' + AND indi_type = '执行' + AND plan_item_name NOT IN ('内部调拨收入','取得贷款') + AND owner_bank_account <> '22195101040012008' +-- 权限控制 + 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 -- 事业部 +-- OR per_code = a.region_code -- 大区 +-- OR per_code = a.office_code -- 办事处 +-- OR per_code = a.city_unit_code -- 城市单元 + ) + ) + ${IF(len(sStart_date_fin) > 0,"AND to_char(stat_date,'yyyy') ='"+ FORMAT(sStart_date_fin,'yyyy') +"'","")} +-- 年 + -- ${IF(sPeriod_fin=1,"AND to_char(stat_date,'yyyy') ='"+ FORMAT(sStart_date_fin,'yyyy') +"'","")} +-- 月 +-- ${IF(sPeriod_fin=2,"AND to_char(stat_date,'yyyy-MM') = '"+ FORMAT(sStart_date_fin,'yyyy-MM') +"'","")} +-- 日 +-- ${IF(sPeriod_fin=3,"AND to_char(stat_date,'yyyy-MM-dd') >= '"+ sStart_date_fin +"'","")} +-- ${IF(sPeriod_fin=4,"AND to_char(stat_date,'yyyy-MM-dd') <= '"+ sStart_date_fin +"'","")} + group by stat_date + UNION ALL + select + stat_date + ,'支出' AS fee_type -- 收支方向 收入/支出 + ,SUM(CASE WHEN trade_direction = '支出' THEN amt ELSE 0 END) / 10000 AS amt + from dm.dm_fim_fud_indicator a + where corp_code not in ('0101','0103','0102') + AND trade_direction = '支出' + AND indi_type = '执行' + AND plan_item_name NOT IN ('内部调拨支出','偿还贷款') + AND owner_bank_account <> '22195101040012008' +-- 权限控制 + 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 -- 事业部 +-- OR per_code = a.region_code -- 大区 +-- OR per_code = a.office_code -- 办事处 +-- OR per_code = a.city_unit_code -- 城市单元 + ) + ) + ${IF(len(sStart_date_fin) > 0,"AND to_char(stat_date,'yyyy') ='"+ FORMAT(sStart_date_fin,'yyyy') +"'","")} +-- 年 + -- ${IF(sPeriod_fin=1,"AND to_char(stat_date,'yyyy') ='"+ FORMAT(sStart_date_fin,'yyyy') +"'","")} +-- 月 +-- ${IF(sPeriod_fin=2,"AND to_char(stat_date,'yyyy-MM') ='"+ FORMAT(sStart_date_fin,'yyyy-MM') +"'","")} +-- 日 +-- ${IF(sPeriod_fin=3,"AND to_char(stat_date,'yyyy-MM-dd') >='"+ sStart_date_fin +"'","")} +-- ${IF(sPeriod_fin=4,"AND to_char(stat_date,'yyyy-MM-dd') <='"+ sStart_date_fin +"'","")} + group by stat_date + UNION ALL +-- 月初资金余额要取上月的月初的,年初要取上年末12月份的 + select + stat_date AS stat_date + ,'余额' AS fee_type + ,sum(amt) / 10000 AS AMT + from dm.dm_fim_fud_balance a + where corp_code not in ('0101','0103','0102') + AND owner_bank_account NOT IN ('20351059900100000512291' + ,'22195101040012008' + ,'信用证保证金' + ,'22195162750000012' + ,'22195162650000017' + ,'22195162350000014' + ,'22195162050000015' + ,'951018033000447470') +-- 权限控制 + 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 -- 事业部 +-- OR per_code = a.region_code -- 大区 +-- OR per_code = a.office_code -- 办事处 +-- OR per_code = a.city_unit_code -- 城市单元 + ) + ) + + ${IF(len(sStart_date_fin) > 0,"AND to_char(stat_date,'yyyy') ='"+ FORMAT(sStart_date_fin,'yyyy') +"'","")} + + +-- 年 + -- ${IF(sPeriod_fin=1,"AND to_char(stat_date,'yyyy') ='"+ FORMAT(sStart_date_fin,'yyyy') +"'","")} +-- 月 +-- ${IF(sPeriod_fin=2,"AND to_char(stat_date,'yyyy-MM') ='"+ FORMAT(MONTHDELTA(sStart_date_fin,-1),'yyyy-MM') +"'","")} +-- 日 +-- ${IF(sPeriod_fin=3,"AND to_char(stat_date,'yyyy-MM-dd') >='"+ sStart_date_fin +"'","")} +-- ${IF(sPeriod_fin=4,"AND to_char(stat_date,'yyyy-MM-dd') <='"+ sStart_date_fin +"'","")} + group by stat_date +)t +ORDER BY to_char(t.stat_date,'MM')