diff --git a/FineReport/财务主题/看板/资金管理驾驶舱.sql b/FineReport/财务主题/看板/资金管理驾驶舱.sql index 8276926..5b152aa 100644 --- a/FineReport/财务主题/看板/资金管理驾驶舱.sql +++ b/FineReport/财务主题/看板/资金管理驾驶舱.sql @@ -764,4 +764,282 @@ WHERE trade_direction IN('收入','支出') GROUP BY to_char(trade_time,'MM') ORDER BY to_char(trade_time,'MM') - ${if(sData_type_out=2,"","*/")} \ No newline at end of file + ${if(sData_type_out=2,"","*/")} + + + +-- ====================================== +-- 资金日报 +-- ====================================== + +-- 期初余额 斌哥处理 + + +-- 资金收入/流入 + +WITH amt_base AS +( +-- 映射日报科目对照关系 + SELECT + stat_date -- 统计日期 + -- todo:期初余额 + -- 其中:可用存款 + -- 银行承兑汇票 + -- 香港公司账户资金 + -- 受限资金 + + -- 收入 + ,SUM(CASE WHEN trade_direction = '收入' AND plan_item_name = '对外销售酒类回款' AND corp_code <> '0201' THEN amt ELSE 0 END) AS finished_in_amt -- 成品酒销售回款 + ,SUM(CASE WHEN trade_direction = '收入' AND plan_item_name = '对外销售酒类回款' AND corp_code = '0201' THEN amt ELSE 0 END) AS loose_in_amt -- 散酒销售回款 + ,SUM(CASE WHEN trade_direction = '收入' AND trade_item IN('共建基金','红包充值费用','转货款','保证金') THEN amt ELSE 0 END) AS redpkg_in_amt -- 红包款/转货款/保证金/共建 + ,SUM(CASE WHEN trade_direction = '收入' AND plan_item_name IN('取得贷款','开票') THEN amt ELSE 0 END) AS bank_in_amt -- 银行融资 + ,SUM(CASE WHEN trade_direction = '收入' AND plan_item_name IN('搬迁补偿/政府补助') THEN amt ELSE 0 END) AS compen_in_amt -- 政府补助 + ,SUM(CASE WHEN trade_direction = '收入' AND plan_item_name IN('对外销售非酒类回款','吸收投资','税费返还','其他收入') AND trade_item not IN('红包充值费用','转货款') THEN amt ELSE 0 END) AS other_in_amt -- 其他零星收款 + -- 支出 + ,SUM(CASE WHEN trade_direction = '支出' AND plan_item_name in('原材料','辅料','低值易耗') THEN amt ELSE 0 END) AS low_mater_out_amt -- 原辅低耗材料 + ,SUM(CASE WHEN trade_direction = '支出' AND plan_item_name in('散酒') THEN amt ELSE 0 END) AS loose_liquor_out_amt -- 外购散酒 + ,SUM(CASE WHEN trade_direction = '支出' AND plan_item_name in('外购包材','外协加工费') THEN amt ELSE 0 END) AS outsourced_out_amt -- 外购包材/外协加工 + ,SUM(CASE WHEN trade_direction = '支出' AND plan_item_name in('其它制造费用','水电气费') THEN amt ELSE 0 END) AS make_out_amt -- 制造费用/能源费用 + ,SUM(CASE WHEN trade_direction = '支出' AND plan_item_name in('广告费','市场活动投入','其它销售费用','委外服务(庄园)','酒店采购','储运费') THEN amt ELSE 0 END) AS ad_out_amt -- 广告/市场活动/庄园采购/储运 + ,SUM(CASE WHEN trade_direction = '支出' AND plan_item_name in('退共建基金','票据变现收入') THEN amt ELSE 0 END) AS build_out_amt -- 共建支出 + ,SUM(CASE WHEN trade_direction = '支出' AND plan_item_name in('退经销商货款') THEN amt ELSE 0 END) AS dealer_out_amt -- 经销商退款 + ,SUM(CASE WHEN trade_direction = '支出' AND plan_item_name in('职工薪酬及福利') THEN amt ELSE 0 END) AS salary_out_amt -- 薪酬及福利 + ,SUM(CASE WHEN trade_direction = '支出' AND plan_item_name in('税费') THEN amt ELSE 0 END) AS tax_out_amt -- 税金缴纳 + ,SUM(CASE WHEN trade_direction = '支出' AND plan_item_name in('其它管理费用','财务手续费用','研发费用') THEN amt ELSE 0 END) AS manage_out_amt -- 管理费用/研发费用 + ,SUM(CASE WHEN trade_direction = '支出' AND plan_item_name in('土地支出') THEN amt ELSE 0 END) AS land_out_amt -- 土地支出 + ,SUM(CASE WHEN trade_direction = '支出' AND plan_item_name in('土建支出') THEN amt ELSE 0 END) AS land_build_out_amt -- 土建支出 + ,SUM(CASE WHEN trade_direction = '支出' AND plan_item_name in('设备支出','维修支出') THEN amt ELSE 0 END) AS repair_out_amt -- 设备及维修支出 + ,SUM(CASE WHEN trade_direction = '支出' AND plan_item_name in('偿还贷款') THEN amt ELSE 0 END) AS repay_out_amt -- 归还贷款 + ,SUM(CASE WHEN trade_direction = '支出' AND plan_item_name in('利息支出') THEN amt ELSE 0 END) AS interest_out_amt -- 利息支出 + ,SUM(CASE WHEN trade_direction = '支出' AND plan_item_name in('分配股利') THEN amt ELSE 0 END) AS dividend_out_amt -- 分配股利 + ,SUM(CASE WHEN trade_direction = '支出' AND plan_item_name in('退保证金','对外捐赠','支付其他代收代付','其他(含未确认支出)','票据变现支出') THEN amt ELSE 0 END) + + -1 * SUM(CASE WHEN trade_direction = '收入' AND plan_item_name in('票据变现收入') THEN amt ELSE 0 END ) + AS other_out_amt -- 其他(捐赠等) + + -- todo:期末余额 + + FROM dm.dm_fim_fud_indicator fp + where fp.trade_direction IN('收入','支出') + and fp.indi_type='执行' + 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 + ) + ) + + AND stat_date >= date '2023-01-01' +GROUP BY stat_date +),amt_base_class AS +( +-- 为方便计算同环比方便,使用扩展方式,而不是计算所有指标 +-- 收入 + SELECT + stat_date + ,'收入' AS fee_class + ,'成品酒销售回款' AS fee_type + ,finished_in_amt AS amt + FROM amt_base + UNION ALL + SELECT + stat_date + ,'收入' AS fee_class + ,'散酒销售回款' as fee_type + ,loose_in_amt AS amt + FROM amt_base + UNION ALL + SELECT + stat_date + ,'收入' AS fee_class + ,'红包款/转货款/保证金/共建' as fee_type + ,redpkg_in_amt AS amt + FROM amt_base + UNION ALL + SELECT + stat_date + ,'收入' AS fee_clas + ,'政府补助' as fee_type + ,compen_in_amt AS amt + FROM amt_base + UNION ALL + SELECT + stat_date + ,'收入' AS fee_class + ,'其他零星收款' as fee_type + ,other_in_amt AS amt + FROM amt_base + UNION ALL + -- 支出 + SELECT + stat_date + ,'支出' AS fee_class + ,'原辅低耗材料' as fee_type + ,low_mater_out_amt AS amt + FROM amt_base + UNION ALL + SELECT + stat_date + ,'支出' AS fee_class + ,'外购散酒' as fee_type + ,loose_liquor_out_amt AS amt + FROM amt_base + UNION ALL + SELECT + stat_date + ,'支出' AS fee_class + ,'外购包材/外协加工' as fee_type + ,outsourced_out_amt AS amt + FROM amt_base + UNION ALL + SELECT + stat_date + ,'支出' AS fee_class + ,'制造费用/能源费用' as fee_type + ,make_out_amt AS amt + FROM amt_base + UNION ALL + SELECT + stat_date + ,'支出' AS fee_class + ,'广告/市场活动/庄园采购/储运' as fee_type + ,ad_out_amt AS amt + FROM amt_base + UNION ALL + SELECT + stat_date + ,'支出' AS fee_class + ,'共建支出' as fee_type + ,build_out_amt AS amt + FROM amt_base + UNION ALL + SELECT + stat_date + ,'支出' AS fee_class + ,'经销商退款' as fee_type + ,dealer_out_amt AS amt + FROM amt_base + UNION ALL + SELECT + stat_date + ,'支出' AS fee_class + ,'薪酬及福利' as fee_type + ,salary_out_amt AS amt + FROM amt_base + UNION ALL + SELECT + stat_date + ,'支出' AS fee_class + ,'税金缴纳' as fee_type + ,tax_out_amt AS amt + FROM amt_base + UNION ALL + SELECT + stat_date + ,'支出' AS fee_class + ,'管理费用/研发费用' as fee_type + ,manage_out_amt AS amt + FROM amt_base + UNION ALL + SELECT + stat_date + ,'支出' AS fee_class + ,'土地支出' as fee_type + ,land_out_amt AS amt + FROM amt_base + UNION ALL + SELECT + stat_date + ,'支出' AS fee_class + ,'土建支出' as fee_type + ,land_build_out_amt AS amt + FROM amt_base + UNION ALL + SELECT + stat_date + ,'支出' AS fee_class + ,'设备及维修支出' as fee_type + ,repair_out_amt AS amt + FROM amt_base + UNION ALL + SELECT + stat_date + ,'支出' AS fee_class + ,'归还贷款' as fee_type + ,repay_out_amt AS amt + FROM amt_base + UNION ALL + SELECT + stat_date + ,'支出' AS fee_class + ,'利息支出' as fee_type + ,interest_out_amt AS amt + FROM amt_base + UNION ALL + SELECT + stat_date + ,'支出' AS fee_class + ,'分配股利' as fee_type + ,dividend_out_amt AS amt + FROM amt_base + UNION ALL + SELECT + stat_date + ,'支出' AS fee_class + ,'其他(捐赠等)' as fee_type + ,other_out_amt AS amt + FROM amt_base +) +SELECT +fee_class +,fee_type +,SUM(CASE WHEN to_char(stat_date,'yyyy-MM-dd') = '${FORMAT(today(),"yyyy-MM-dd")}' + THEN amt ELSE 0 END) / 10000 AS today_amt -- 本日 +,SUM(CASE WHEN to_char(stat_date,'yyyy-MM') = '${FORMAT(today(),"yyyy-MM")}' + THEN amt ELSE 0 END) / 10000 AS month_amt -- 本月累计 +,SUM(CASE WHEN to_char(stat_date,'yyyy') = '${FORMAT(today(),"yyyy")}' + THEN amt ELSE 0 END) / 10000 AS year_amt -- 本年累计 +,SUM(CASE WHEN to_char(stat_date,'yyyy') = '${FORMAT(YEARDELTA(today(),-1),"yyyy")}' + THEN amt ELSE 0 END) / 10000 AS last_year_amt -- 去年同期累计 +FROM amt_base_class +GROUP BY fee_type,fee_class +ORDER BY +decode(fee_type +,'成品酒销售回款','a' +,'散酒销售回款','b' +,'红包款/转货款/保证金/共建','c' +,'银行融资','d' +,'政府补助','e' +,'其他零星收款','f' +,'原辅低耗材料','g' +,'外购散酒','h' +,'外购包材/外协加工','i' +,'制造费用/能源费用','j' +,'广告/市场活动/庄园采购/储运','k' +,'共建支出','l' +,'经销商退款','m' +,'薪酬及福利','n' +,'税金缴纳','o' +,'管理费用/研发费用','p' +,'土地支出','q' +,'土建支出','r' +,'设备及维修支出','m' +,'归还贷款','n' +,'利息支出','o' +,'分配股利','v' +,'其他(捐赠等)','w' +) + + + + +-- 期末余额 + + diff --git a/FineReport/销售主题/报表/庄园之旅场次.sql b/FineReport/销售主题/报表/庄园之旅场次.sql new file mode 100644 index 0000000..a821e4c --- /dev/null +++ b/FineReport/销售主题/报表/庄园之旅场次.sql @@ -0,0 +1,96 @@ +--庄园之旅场次 +select +t1.deal_code as deal_code -- 经销商编码 +,t1.team_name AS deal_name +,dmd.fist_dept_code as bu_code -- 事业部编码 +,dmd.fist_dept_name as bu_name -- 事业部 +,dmd.secd_dept_code as region_code -- 大区编码 +,dmd.secd_dept_name as region_name -- 大区 +,dmd.thrd_dept_code as office_code -- 办事处编码 +,dmd.thrd_dept_name as office_name -- 办事处 +,dmd.dept_code as city_unit_code -- 城市单元编码 +,dmd.dept_name as city_unit_name -- 城市单元 +,count(distinct t1.team_code) as session_num -- 场次 +,SUM(t2.num) AS num -- 人数 +,SUM(t1.budget_amt) AS budget_amt -- 预算费用 +,SUM(t1.visit_fee) AS visit_fee -- 参观费用 +,SUM(t1.exp_fee) AS speak_fee -- 解说费用 +from dwi.dwi_mak_estmate_apply t1 +LEFT JOIN +( + SELECT + team_code + ,COUNT(*) AS num + FROM dwi.dwi_mak_estmate_guest + GROUP by team_code +)t2 +ON t1.team_code = t2.team_code +left join dwr.dim_department d +on t1.dept_code=d.dept_code +left join +( + select deal_code,bu_code,max(city_unit_code) as city_unit_code + from dwr.dim_dealer_administer + where enabled_status=1 + group by deal_code, bu_code +) dda +on t1.deal_code=dda.deal_code +and if(d.fist_dept_code in('0202/0215','0202/0216'),'0202/0226',d.fist_dept_code)=dda.bu_code +left join dwr.dim_department dmd on nvl(dda.city_unit_code,d.dept_code)=dmd.dept_code +where team_type='庄园之旅' +and bill_status='已审核' +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 = dmd.fist_dept_code + OR per_code = dmd.secd_dept_code + OR per_code = dmd.thrd_dept_code + OR per_code = dmd.dept_code + ) +) +${if(len(sBu) == 0,"","and dmd.fist_dept_code IN ('"+sBu+"')")} +${if(len(sRegion) == 0,"","and dmd.secd_dept_code IN ('"+sRegion+"')")} +${if(len(sOffice) == 0,"","and dmd.thrd_dept_code IN ('"+sOffice+"')")} +${if(len(sCu) == 0,"","and dmd.dept_code IN ('"+sCu+"')")} +${IF(sDeal='',"","AND t1.deal_code IN ('"+sDeal+"')")} + +and trip_end_date <= date '${sEnd_date}' +GROUP BY t1.deal_code, t1.team_name, dmd.fist_dept_code, dmd.fist_dept_name, dmd.secd_dept_code, dmd.secd_dept_name, dmd.thrd_dept_code, dmd.thrd_dept_name, dmd.dept_code, dmd.dept_name + + + + +SELECT +t1.team_code AS team_code -- 团队id +,COUNT ( * ) AS num +FROM dwi.dwi_mak_estmate_guest t1 + INNER JOIN dwi.dwi_mak_estmate_apply t2 ON t1.team_code = t2.team_code AND t2.bill_status = '已审核' + LEFT JOIN dwr.dim_department d ON t2.dept_code = d.dept_code + LEFT JOIN ( + SELECT + deal_code, + bu_code, + MAX ( city_unit_code ) AS city_unit_code + FROM + dwr.dim_dealer_administer + WHERE + enabled_status = 1 + GROUP BY + deal_code, + bu_code ) dda ON t2.deal_code = dda.deal_code AND IF( d.fist_dept_code IN ( '0202/0215', '0202/0216' ), '0202/0226', d.fist_dept_code ) = dda.bu_code + LEFT JOIN dwr.dim_department dmd ON nvl ( dda.city_unit_code, d.dept_code ) = dmd.dept_code +WHERE + t2.team_type = '庄园之旅' + AND t2.bill_status = '已审核' + + ${IF(LEN(sCu) = 0,"","AND dmd.dept_code IN ('"+sCu+"')")} + + ${IF(LEN(sStart_date) = 0,"","AND t2.trip_start_date >= '"+sStart_date+"'")} + ${IF(LEN(sEnd_date) = 0,"","AND t2.trip_start_date <= '"+sEnd_date+"'")} \ No newline at end of file diff --git a/FineReport/销售主题/看板/销售管理驾驶舱-市场费用.sql b/FineReport/销售主题/看板/销售管理驾驶舱-市场费用.sql new file mode 100644 index 0000000..1a3ca75 --- /dev/null +++ b/FineReport/销售主题/看板/销售管理驾驶舱-市场费用.sql @@ -0,0 +1,1803 @@ +-- ====================================== +-- report_市场费用_费用执行 +-- ====================================== + + +select + '当期' time_type, + ${IF(sPeriod=1,"to_char(stat_date,'yyyy-MM') AS stat_time,","")} + ${IF(sPeriod=2,"CASE WHEN MONTH(stat_date)<=3 THEN concat(left(stat_date,4),'-1') + WHEN MONTH(stat_date)<=6 AND MONTH(stat_date)>=4 THEN concat(left(stat_date,4),'-2') + WHEN MONTH(stat_date)<=9 AND MONTH(stat_date)>=7 THEN concat(left(stat_date,4),'-3') + WHEN MONTH(stat_date)<=12 AND MONTH(stat_date)>=10 THEN concat(left(stat_date,4),'-4') + ELSE NULL + END AS stat_time,","")} + ${IF(sPeriod=3,"to_char(stat_date,'yyyy') AS stat_time,","")} + bu_name, + sum(amt) AS amt +from dm.v_sal_market_indi_month a +where (indi_type='费用核报') + ${IF(LEN(sBu)=0,"","AND bu_code IN ('"+sBu+"')")} + ${IF(LEN(sRegion)=0,"","AND region_code IN ('"+sRegion+"')")} +AND to_char(stat_date,'yyyy-MM')>='${sStart_mon}' +AND to_char(stat_date,'yyyy-MM')<='${sEnd_mon}' +AND EXISTS ( +SELECT + 1 +FROM + dm.dm_bi_user_permisson +WHERE + bi_user = '${fine_username}' -- 替换为fine_username + AND per_model IN ('all','sal','fim') + 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 + ) +) +GROUP BY + stat_time, + bu_name + +UNION ALL +select + '同期' time_type, + ${IF(sPeriod=1,"to_char(stat_date,'yyyy-MM') AS stat_time,","")} + ${IF(sPeriod=2,"CASE WHEN MONTH(stat_date)<=3 THEN concat(left(stat_date,4),'-1') + WHEN MONTH(stat_date)<=6 AND MONTH(stat_date)>=4 THEN concat(left(stat_date,4),'-2') + WHEN MONTH(stat_date)<=9 AND MONTH(stat_date)>=7 THEN concat(left(stat_date,4),'-3') + WHEN MONTH(stat_date)<=12 AND MONTH(stat_date)>=10 THEN concat(left(stat_date,4),'-4') + ELSE NULL + END AS stat_time,","")} + ${IF(sPeriod=3,"to_char(stat_date,'yyyy') AS stat_time,","")} + bu_name, + sum(amt) AS amt +from dm.v_sal_market_indi_month a +where (indi_type='费用核报') + ${IF(LEN(sBu)=0,"","AND bu_code IN ('"+sBu+"')")} + ${IF(LEN(sRegion)=0,"","AND region_code IN ('"+sRegion+"')")} +AND to_char(stat_date,'yyyy-MM')>='${pPrevious_s}' +AND to_char(stat_date,'yyyy-MM')<='${pPrevious_e}' +AND EXISTS ( +SELECT + 1 +FROM + dm.dm_bi_user_permisson +WHERE + bi_user = '${fine_username}' -- 替换为fine_username + AND per_model IN ('all','sal','fim') + 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 + ) +) +GROUP BY + stat_time, + bu_name + + +-- ====================================== +-- report_市场费用_费用预算 +-- ====================================== + +select + '当期' time_type, + ${IF(sPeriod=1,"to_char(stat_date,'yyyy-MM') AS stat_time,","")} + ${IF(sPeriod=2,"CASE WHEN MONTH(stat_date)<=3 THEN concat(left(stat_date,4),'-1') + WHEN MONTH(stat_date)<=6 AND MONTH(stat_date)>=4 THEN concat(left(stat_date,4),'-2') + WHEN MONTH(stat_date)<=9 AND MONTH(stat_date)>=7 THEN concat(left(stat_date,4),'-3') + WHEN MONTH(stat_date)<=12 AND MONTH(stat_date)>=10 THEN concat(left(stat_date,4),'-4') + ELSE NULL + END AS stat_time,","")} + ${IF(sPeriod=3,"to_char(stat_date,'yyyy') AS stat_time,","")} + bu_name, + sum(amt) AS amt +from dm.v_sal_market_indi_month a +where (indi_type='期初预算' OR indi_type='调整预算') + ${IF(LEN(sBu)=0,"","AND bu_code IN ('"+sBu+"')")} + ${IF(LEN(sRegion)=0,"","AND region_code IN ('"+sRegion+"')")} +AND to_char(stat_date,'yyyy')='${YEAR(sEnd_mon)}' +AND EXISTS ( +SELECT + 1 +FROM + dm.dm_bi_user_permisson +WHERE + bi_user = '${fine_username}' -- 替换为fine_username + AND per_model IN ('all','sal','fim') + 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 + ) +) +GROUP BY + stat_time, + bu_name + +UNION ALL + +select + '同期' time_type, + ${IF(sPeriod=1,"to_char(stat_date,'yyyy-MM') AS stat_time,","")} + ${IF(sPeriod=2,"CASE WHEN MONTH(stat_date)<=3 THEN concat(left(stat_date,4),'-1') + WHEN MONTH(stat_date)<=6 AND MONTH(stat_date)>=4 THEN concat(left(stat_date,4),'-2') + WHEN MONTH(stat_date)<=9 AND MONTH(stat_date)>=7 THEN concat(left(stat_date,4),'-3') + WHEN MONTH(stat_date)<=12 AND MONTH(stat_date)>=10 THEN concat(left(stat_date,4),'-4') + ELSE NULL + END AS stat_time,","")} + ${IF(sPeriod=3,"to_char(stat_date,'yyyy') AS stat_time,","")} + bu_name, + sum(amt) AS amt +from dm.v_sal_market_indi_month a +where (indi_type='期初预算' OR indi_type='调整预算') + ${IF(LEN(sBu)=0,"","AND bu_code IN ('"+sBu+"')")} + ${IF(LEN(sRegion)=0,"","AND region_code IN ('"+sRegion+"')")} +AND to_char(stat_date,'yyyy')='${YEAR(sEnd_mon)-1}' +AND EXISTS ( +SELECT + 1 +FROM + dm.dm_bi_user_permisson +WHERE + bi_user = '${fine_username}' -- 替换为fine_username + AND per_model IN ('all','sal','fim') + 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 + ) +) +GROUP BY + stat_time, + bu_name + +-- ====================================== +-- report_市场费用_方案规划 +-- ====================================== + +select + '当期' time_type, + ${IF(sPeriod=1,"to_char(stat_date,'yyyy-MM') AS stat_time,","")} + ${IF(sPeriod=2,"CASE WHEN MONTH(stat_date)<=3 THEN concat(left(stat_date,4),'-1') + WHEN MONTH(stat_date)<=6 AND MONTH(stat_date)>=4 THEN concat(left(stat_date,4),'-2') + WHEN MONTH(stat_date)<=9 AND MONTH(stat_date)>=7 THEN concat(left(stat_date,4),'-3') + WHEN MONTH(stat_date)<=12 AND MONTH(stat_date)>=10 THEN concat(left(stat_date,4),'-4') + ELSE NULL + END AS stat_time,","")} + ${IF(sPeriod=3,"to_char(stat_date,'yyyy') AS stat_time,","")} + bu_name, + sum(amt) AS amt +from dm.v_sal_market_indi_month a +where (indi_type='方案规划') + ${IF(LEN(sBu)=0,"","AND bu_code IN ('"+sBu+"')")} + ${IF(LEN(sRegion)=0,"","AND region_code IN ('"+sRegion+"')")} +AND to_char(stat_date,'yyyy-MM')>='${sStart_mon}' +AND to_char(stat_date,'yyyy-MM')<='${sEnd_mon}' +AND EXISTS ( +SELECT + 1 +FROM + dm.dm_bi_user_permisson +WHERE + bi_user = '${fine_username}' -- 替换为fine_username + AND per_model IN ('all','sal','fim') + 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 + ) +) +GROUP BY + stat_time, + bu_name + +UNION ALL + +select + '同期' time_type, + ${IF(sPeriod=1,"to_char(stat_date,'yyyy-MM') AS stat_time,","")} + ${IF(sPeriod=2,"CASE WHEN MONTH(stat_date)<=3 THEN concat(left(stat_date,4),'-1') + WHEN MONTH(stat_date)<=6 AND MONTH(stat_date)>=4 THEN concat(left(stat_date,4),'-2') + WHEN MONTH(stat_date)<=9 AND MONTH(stat_date)>=7 THEN concat(left(stat_date,4),'-3') + WHEN MONTH(stat_date)<=12 AND MONTH(stat_date)>=10 THEN concat(left(stat_date,4),'-4') + ELSE NULL + END AS stat_time,","")} + ${IF(sPeriod=3,"to_char(stat_date,'yyyy') AS stat_time,","")} + bu_name, + sum(amt) AS amt +from dm.v_sal_market_indi_month a +where (indi_type='方案规划') + ${IF(LEN(sBu)=0,"","AND bu_code IN ('"+sBu+"')")} + ${IF(LEN(sRegion)=0,"","AND region_code IN ('"+sRegion+"')")} +AND to_char(stat_date,'yyyy-MM')>='${pPrevious_s}' +AND to_char(stat_date,'yyyy-MM')<='${pPrevious_e}' +AND EXISTS ( +SELECT + 1 +FROM + dm.dm_bi_user_permisson +WHERE + bi_user = '${fine_username}' -- 替换为fine_username + AND per_model IN ('all','sal','fim') + 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 + ) +) +GROUP BY + stat_time, + bu_name + + +-- ====================================== +-- report_市场费用_回款 +-- ====================================== + +-- 事业部层级取数 +${if(sLevel=1,"","/*")} +select + '当期' time_type, + ${IF(sPeriod=1,"to_char(stat_date,'yyyy-MM') AS stat_time,","")} + ${IF(sPeriod=2,"CASE WHEN MONTH(stat_date)<=3 THEN concat(left(stat_date,4),'-1') + WHEN MONTH(stat_date)<=6 AND MONTH(stat_date)>=4 THEN concat(left(stat_date,4),'-2') + WHEN MONTH(stat_date)<=9 AND MONTH(stat_date)>=7 THEN concat(left(stat_date,4),'-3') + WHEN MONTH(stat_date)<=12 AND MONTH(stat_date)>=10 THEN concat(left(stat_date,4),'-4') + ELSE NULL + END AS stat_time,","")} + ${IF(sPeriod=3,"to_char(stat_date,'yyyy') AS stat_time,","")} + replace(bu_name,'事业部','') AS bu_name, + sum(amt_bu) AS amt +from dm.v_sal_indicator_bu a +where (indi_type='回款' OR indi_type='电商订单') + ${IF(LEN(sBu)=0,"","AND bu_code IN ('"+sBu+"')")} + AND to_char(stat_date,'yyyy-MM')>='${sStart_mon}' + AND to_char(stat_date,'yyyy-MM')<='${sEnd_mon}' + AND EXISTS ( + SELECT + 1 + FROM + dm.dm_bi_user_permisson + WHERE + bi_user = '${fine_username}' -- 替换为fine_username + AND per_model IN ('all','sal','fim') + AND ( + is_all = 1 + OR per_code = a.bu_code + ) +) +GROUP BY + stat_time, + bu_name + +--UNION ALL +-- +--select +--'同期' time_type, +--${IF(sPeriod=1,"to_char(stat_date,'yyyy-MM') AS stat_time,","")} +--${IF(sPeriod=2,"CASE WHEN MONTH(stat_date)<=3 THEN concat(left(stat_date,4),'-1') +--WHEN MONTH(stat_date)<=6 AND MONTH(stat_date)>=4 THEN concat(left(stat_date,4),'-2') +--WHEN MONTH(stat_date)<=9 AND MONTH(stat_date)>=7 THEN concat(left(stat_date,4),'-3') +--WHEN MONTH(stat_date)<=12 AND MONTH(stat_date)>=10 THEN concat(left(stat_date,4),'-4') +--ELSE NULL +--END AS stat_time,","")} +--${IF(sPeriod=3,"to_char(stat_date,'yyyy') AS stat_time,","")} +--replace(bu_name,'事业部','') AS bu_name, +--sum(amt_bu) AS amt +--from dm.v_sal_indicator_bu +--where (indi_type='回款' OR indi_type='电商订单') +--${IF(LEN(sBu)=0,"","AND bu_code IN ('"+sBu+"')")} +--${IF(sPeriod=1,"AND stat_time>='"+pPrevious_s+"'","")} +--${IF(sPeriod=1,"AND stat_time<='"+pPrevious_e+"'","")} +--${IF(sPeriod=2,"AND stat_time>='"+pPrevious_s+"'","")} +--${IF(sPeriod=2,"AND stat_time<='"+pPrevious_e+"'","")} +--${IF(sPeriod=3,"AND stat_time>='"+pPrevious_s+"'","")} +--${IF(sPeriod=3,"AND stat_time<='"+pPrevious_e+"'","")} +--GROUP BY +--stat_time, +--bu_name + +${if(sLevel=1,"","*/")} + + +-- 事业部大区取数 +${if(sLevel=2,"","/*")} + +select + '当期' time_type, + ${IF(sPeriod=1,"to_char(stat_date,'yyyy-MM') AS stat_time,","")} + ${IF(sPeriod=2,"CASE WHEN MONTH(stat_date)<=3 THEN concat(left(stat_date,4),'-1') + WHEN MONTH(stat_date)<=6 AND MONTH(stat_date)>=4 THEN concat(left(stat_date,4),'-2') + WHEN MONTH(stat_date)<=9 AND MONTH(stat_date)>=7 THEN concat(left(stat_date,4),'-3') + WHEN MONTH(stat_date)<=12 AND MONTH(stat_date)>=10 THEN concat(left(stat_date,4),'-4') + ELSE NULL + END AS stat_time,","")} + ${IF(sPeriod=3,"to_char(stat_date,'yyyy') AS stat_time,","")} + replace(bu_name,'事业部','') AS bu_name, + sum(amt_region) AS amt +from dm.v_sal_indicator_region a +where (indi_type='回款' OR indi_type='电商订单') + AND to_char(stat_date,'yyyy-MM')>='${sStart_mon}' + AND to_char(stat_date,'yyyy-MM')<='${sEnd_mon}' + ${IF(LEN(sBu)=0,"","AND bu_code IN ('"+sBu+"')")} +${IF(LEN(sRegion)=0,"","AND region_code IN ('"+sRegion+"')")} + AND EXISTS ( + SELECT + 1 + FROM + dm.dm_bi_user_permisson + WHERE + bi_user = '${fine_username}' -- 替换为fine_username + AND per_model IN ('all','sal','fim') + AND ( + is_all = 1 + OR per_code = a.bu_code + ) +) +GROUP BY + stat_time, + bu_name + +${if(sLevel=2,"","*/")} + + + + +-- ====================================== +-- report_市场费用_出货 +-- ====================================== +-- 事业部层级取数 + + +${if(sLevel=1,"","/*")} +select + '当期' time_type, + ${IF(sPeriod=1,"to_char(stat_date,'yyyy-MM') AS stat_time,","")} + ${IF(sPeriod=2,"CASE WHEN MONTH(stat_date)<=3 THEN concat(left(stat_date,4),'-1') + WHEN MONTH(stat_date)<=6 AND MONTH(stat_date)>=4 THEN concat(left(stat_date,4),'-2') + WHEN MONTH(stat_date)<=9 AND MONTH(stat_date)>=7 THEN concat(left(stat_date,4),'-3') + WHEN MONTH(stat_date)<=12 AND MONTH(stat_date)>=10 THEN concat(left(stat_date,4),'-4') + ELSE NULL + END AS stat_time,","")} + ${IF(sPeriod=3,"to_char(stat_date,'yyyy') AS stat_time,","")} + replace(bu_name,'事业部','') AS bu_name, + sum(amt_bu) AS amt +from dm.v_sal_indicator_bu a +where (indi_type='出货' OR indi_type='电商订单') + ${IF(LEN(sBu)=0,"","AND bu_code IN ('"+sBu+"')")} + + AND to_char(stat_date,'yyyy-MM')>='${sStart_mon}' +AND to_char(stat_date,'yyyy-MM')<='${sEnd_mon}' +AND EXISTS ( +SELECT + 1 +FROM + dm.dm_bi_user_permisson +WHERE + bi_user = '${fine_username}' -- 替换为fine_username + AND per_model IN ('all','sal','fim') + AND ( + is_all = 1 + OR per_code = a.bu_code + ) +) +GROUP BY + stat_time, + bu_name + +UNION ALL + +select + '同期' time_type, + ${IF(sPeriod=1,"to_char(stat_date,'yyyy-MM') AS stat_time,","")} + ${IF(sPeriod=2,"CASE WHEN MONTH(stat_date)<=3 THEN concat(left(stat_date,4),'-1') + WHEN MONTH(stat_date)<=6 AND MONTH(stat_date)>=4 THEN concat(left(stat_date,4),'-2') + WHEN MONTH(stat_date)<=9 AND MONTH(stat_date)>=7 THEN concat(left(stat_date,4),'-3') + WHEN MONTH(stat_date)<=12 AND MONTH(stat_date)>=10 THEN concat(left(stat_date,4),'-4') + ELSE NULL + END AS stat_time,","")} + ${IF(sPeriod=3,"to_char(stat_date,'yyyy') AS stat_time,","")} + replace(bu_name,'事业部','') AS bu_name, + sum(amt_bu) AS amt +from dm.v_sal_indicator_bu a +where (indi_type='出货' OR indi_type='电商订单') + ${IF(LEN(sBu)=0,"","AND bu_code IN ('"+sBu+"')")} + + AND to_char(stat_date,'yyyy-MM')>='${pPrevious_s}' +AND to_char(stat_date,'yyyy-MM')<='${pPrevious_e}' +AND EXISTS ( +SELECT + 1 +FROM + dm.dm_bi_user_permisson +WHERE + bi_user = '${fine_username}' -- 替换为fine_username + AND per_model IN ('all','sal','fim') + AND ( + is_all = 1 + OR per_code = a.bu_code + ) +) +GROUP BY + stat_time, + bu_name + +${if(sLevel=1,"","*/")} + + +-- 事业部大区取数 +${if(sLevel=2,"","/*")} + +select + '当期' time_type, + ${IF(sPeriod=1,"to_char(stat_date,'yyyy-MM') AS stat_time,","")} + ${IF(sPeriod=2,"CASE WHEN MONTH(stat_date)<=3 THEN concat(left(stat_date,4),'-1') + WHEN MONTH(stat_date)<=6 AND MONTH(stat_date)>=4 THEN concat(left(stat_date,4),'-2') + WHEN MONTH(stat_date)<=9 AND MONTH(stat_date)>=7 THEN concat(left(stat_date,4),'-3') + WHEN MONTH(stat_date)<=12 AND MONTH(stat_date)>=10 THEN concat(left(stat_date,4),'-4') + ELSE NULL + END AS stat_time,","")} + ${IF(sPeriod=3,"to_char(stat_date,'yyyy') AS stat_time,","")} + replace(bu_name,'事业部','') AS bu_name, + sum(amt_region) AS amt +from dm.v_sal_indicator_region a +where (indi_type='出货' OR indi_type='电商订单') + ${IF(LEN(sBu)=0,"","AND bu_code IN ('"+sBu+"')")} + ${IF(LEN(sRegion)=0,"","AND region_code IN ('"+sRegion+"')")} +AND to_char(stat_date,'yyyy-MM')>='${sStart_mon}' +AND to_char(stat_date,'yyyy-MM')<='${sEnd_mon}' +AND EXISTS ( +SELECT + 1 +FROM + dm.dm_bi_user_permisson +WHERE + bi_user = '${fine_username}' -- 替换为fine_username + AND per_model IN ('all','sal','fim') + AND ( + is_all = 1 + OR per_code = a.bu_code + ) +) +GROUP BY + stat_time, + bu_name + +UNION ALL + +select + '同期' time_type, + ${IF(sPeriod=1,"to_char(stat_date,'yyyy-MM') AS stat_time,","")} + ${IF(sPeriod=2,"CASE WHEN MONTH(stat_date)<=3 THEN concat(left(stat_date,4),'-1') + WHEN MONTH(stat_date)<=6 AND MONTH(stat_date)>=4 THEN concat(left(stat_date,4),'-2') + WHEN MONTH(stat_date)<=9 AND MONTH(stat_date)>=7 THEN concat(left(stat_date,4),'-3') + WHEN MONTH(stat_date)<=12 AND MONTH(stat_date)>=10 THEN concat(left(stat_date,4),'-4') + ELSE NULL + END AS stat_time,","")} + ${IF(sPeriod=3,"to_char(stat_date,'yyyy') AS stat_time,","")} + replace(bu_name,'事业部','') AS bu_name, + sum(amt_region) AS amt +from dm.v_sal_indicator_region a +where (indi_type='出货' OR indi_type='电商订单') + ${IF(LEN(sBu)=0,"","AND bu_code IN ('"+sBu+"')")} + ${IF(LEN(sRegion)=0,"","AND region_code IN ('"+sRegion+"')")} +AND to_char(stat_date,'yyyy-MM')>='${pPrevious_s}' +AND to_char(stat_date,'yyyy-MM')<='${pPrevious_e}' +AND EXISTS ( +SELECT + 1 +FROM + dm.dm_bi_user_permisson +WHERE + bi_user = '${fine_username}' -- 替换为fine_username + AND per_model IN ('all','sal','fim') + AND ( + is_all = 1 + OR per_code = a.bu_code + ) +) +GROUP BY + stat_time, + bu_name + + + +${if(sLevel=2,"","*/")} + + +-- ====================================== +-- chart_市场费用_项目预算执行 +-- ====================================== + + +with tb as (select + ${IF(sPeriod=1,"to_char(stat_date,'yyyy-MM') AS stat_time,","")} + ${IF(sPeriod=2,"CASE WHEN MONTH(stat_date)<=3 THEN concat(left(stat_date,4),'-1') + WHEN MONTH(stat_date)<=6 AND MONTH(stat_date)>=4 THEN concat(left(stat_date,4),'-2') + WHEN MONTH(stat_date)<=9 AND MONTH(stat_date)>=7 THEN concat(left(stat_date,4),'-3') + WHEN MONTH(stat_date)<=12 AND MONTH(stat_date)>=10 THEN concat(left(stat_date,4),'-4') + ELSE NULL + END AS stat_time,","")} + ${IF(sPeriod=3,"to_char(stat_date,'yyyy') AS stat_time,","")} + bu_name, + fist_item, + sum(amt) AS amt_ys, --预算金额 + 0 as amt_bx --执行金额 + from dm.v_sal_market_indi_month a + where (indi_type='期初预算' OR indi_type='调整预算') + ${IF(LEN(sBu)=0,"","AND bu_code IN ('"+sBu+"')")} + ${IF(LEN(sRegion)=0,"","AND region_code IN ('"+sRegion+"')")} +AND to_char(stat_date,'yyyy')>='${YEAR(sEnd_mon)}' +AND EXISTS ( +SELECT + 1 +FROM + dm.dm_bi_user_permisson +WHERE + bi_user = '${fine_username}' -- 替换为fine_username + AND per_model IN ('all','sal','fim') + 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 + ) +) + GROUP BY + stat_time, + bu_name, + fist_item + + + UNION ALL + + select + ${IF(sPeriod=1,"to_char(stat_date,'yyyy-MM') AS stat_time,","")} + ${IF(sPeriod=2,"CASE WHEN MONTH(stat_date)<=3 THEN concat(left(stat_date,4),'-1') +WHEN MONTH(stat_date)<=6 AND MONTH(stat_date)>=4 THEN concat(left(stat_date,4),'-2') +WHEN MONTH(stat_date)<=9 AND MONTH(stat_date)>=7 THEN concat(left(stat_date,4),'-3') +WHEN MONTH(stat_date)<=12 AND MONTH(stat_date)>=10 THEN concat(left(stat_date,4),'-4') +ELSE NULL +END AS stat_time,","")} + ${IF(sPeriod=3,"to_char(stat_date,'yyyy') AS stat_time,","")} + bu_name, + fist_item, + 0 as amt_ys, --预算金额 + sum(amt) AS amt_bx --执行金额 + from dm.v_sal_market_indi_month a + where (indi_type='费用核报') + ${IF(LEN(sBu)=0,"","AND bu_code IN ('"+sBu+"')")} + ${IF(LEN(sRegion)=0,"","AND region_code IN ('"+sRegion+"')")} + AND to_char(stat_date,'yyyy-MM')>='${sStart_mon}' + AND to_char(stat_date,'yyyy-MM')<='${sEnd_mon}' + AND EXISTS ( + SELECT + 1 + FROM + dm.dm_bi_user_permisson + WHERE + bi_user = '${fine_username}' -- 替换为fine_username + AND per_model IN ('all','sal','fim') + 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 + ) + ) + GROUP BY + stat_time, + bu_name, + fist_item +) + ,tb1 AS (select + fist_item, + sum(amt_ys) as sum_amt_ys, --预算金额 + sum(amt_bx) AS sum_amt_bx --执行金额 + from tb + GROUP BY + fist_item +) + ,tb2 as (select + sum(amt_ys) as amt_ys1 + from tb +) +select + a.*, + b.amt_ys1, + ${if(pDim_market=1,"'执行率' AS type,","'占比' AS type,")} + ${if(pDim_market=1,"case when a.sum_amt_ys = 0 THEN 0 else a.sum_amt_bx/a.sum_amt_ys end AS rate","CASE WHEN b.amt_ys1 = 0 THEN 0 ELSE a.sum_amt_ys/b.amt_ys1 END AS rate")} +from tb1 a + left join tb2 b + ON 1=1 + + +-- ====================================== +-- chart_市场费用_三级项目预算执行top10 +-- ====================================== + +with tb as (select + ${IF(sPeriod=1,"to_char(stat_date,'yyyy-MM') AS stat_time,","")} + ${IF(sPeriod=2,"CASE WHEN MONTH(stat_date)<=3 THEN concat(left(stat_date,4),'-1') + WHEN MONTH(stat_date)<=6 AND MONTH(stat_date)>=4 THEN concat(left(stat_date,4),'-2') + WHEN MONTH(stat_date)<=9 AND MONTH(stat_date)>=7 THEN concat(left(stat_date,4),'-3') + WHEN MONTH(stat_date)<=12 AND MONTH(stat_date)>=10 THEN concat(left(stat_date,4),'-4') + ELSE NULL + END AS stat_time,","")} + ${IF(sPeriod=3,"to_char(stat_date,'yyyy') AS stat_time,","")} + bu_name, + fist_item, + cost_item, + pay_way, + sum(amt) AS amt_ys, --预算金额 + 0 as amt_bx --执行金额 + from dm.v_sal_market_indi_month a + where (indi_type='期初预算' OR indi_type='调整预算') + ${IF(LEN(sBu)=0,"","AND bu_code IN ('"+sBu+"')")} + ${IF(LEN(sRegion)=0,"","AND region_code IN ('"+sRegion+"')")} +AND to_char(stat_date,'yyyy')='${YEAR(sEnd_mon)}' +AND EXISTS ( +SELECT + 1 +FROM + dm.dm_bi_user_permisson +WHERE + bi_user = '${fine_username}' -- 替换为fine_username + AND per_model IN ('all','sal','fim') + 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 + ) +) + GROUP BY + stat_time, + bu_name, + fist_item, + cost_item, + pay_way + + UNION ALL + + select + ${IF(sPeriod=1,"to_char(stat_date,'yyyy-MM') AS stat_time,","")} + ${IF(sPeriod=2,"CASE WHEN MONTH(stat_date)<=3 THEN concat(left(stat_date,4),'-1') +WHEN MONTH(stat_date)<=6 AND MONTH(stat_date)>=4 THEN concat(left(stat_date,4),'-2') +WHEN MONTH(stat_date)<=9 AND MONTH(stat_date)>=7 THEN concat(left(stat_date,4),'-3') +WHEN MONTH(stat_date)<=12 AND MONTH(stat_date)>=10 THEN concat(left(stat_date,4),'-4') +ELSE NULL +END AS stat_time,","")} + ${IF(sPeriod=3,"to_char(stat_date,'yyyy') AS stat_time,","")} + bu_name, + fist_item, + cost_item, + pay_way, + 0 as amt_ys, --预算金额 + sum(amt) AS amt_bx --执行金额 + from dm.v_sal_market_indi_month a + where (indi_type='费用核报') + ${IF(LEN(sRegion)=0,"","AND region_code IN ('"+sRegion+"')")} + ${IF(LEN(sBu)=0,"","AND bu_code IN ('"+sBu+"')")} + AND to_char(stat_date,'yyyy-MM')>='${sStart_mon}' + AND to_char(stat_date,'yyyy-MM')<='${sEnd_mon}' + AND EXISTS ( + SELECT + 1 + FROM + dm.dm_bi_user_permisson + WHERE + bi_user = '${fine_username}' -- 替换为fine_username + AND per_model IN ('all','sal','fim') + 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 + ) + ) + GROUP BY + stat_time, + bu_name, + fist_item, + cost_item, + pay_way +) + ,tb1 AS (select + cost_item, + sum(amt_ys) as sum_amt_ys, --预算金额 + sum(amt_bx) AS sum_amt_bx --执行金额 + from tb + GROUP BY + cost_item +) +select * from tb1 ORDER BY sum_amt_bx desc limit 10 + + + +-- ====================================== +-- chart_市场费用_支付方式占比 +-- ====================================== + +with tb as ( +--select +--${IF(sPeriod=1,"to_char(stat_date,'yyyy-MM') AS stat_time,","")} +--${IF(sPeriod=2,"CASE WHEN MONTH(stat_date)<=3 THEN concat(left(stat_date,4),'-1') +--WHEN MONTH(stat_date)<=6 AND MONTH(stat_date)>=4 THEN concat(left(stat_date,4),'-2') +--WHEN MONTH(stat_date)<=9 AND MONTH(stat_date)>=7 THEN concat(left(stat_date,4),'-3') +--WHEN MONTH(stat_date)<=12 AND MONTH(stat_date)>=10 THEN concat(left(stat_date,4),'-4') +--ELSE NULL +--END AS stat_time,","")} +--${IF(sPeriod=3,"to_char(stat_date,'yyyy') AS stat_time,","")} +--bu_name, +--fist_item, +--cost_item, +--pay_way, +--sum(amt) AS amt_ys, --预算金额 +--0 as amt_bx --执行金额 +--from dm.v_sal_market_indi_month +--where (indi_type='期初预算' OR indi_type='调整预算') +--${IF(LEN(sBu)=0,"","AND bu_code IN ('"+sBu+"')")} +--${IF(sPeriod=1,"AND stat_time>='"+sStart_ym+"'","")} +--${IF(sPeriod=1,"AND stat_time<='"+sEnd_ym+"'","")} +--${IF(sPeriod=2,"AND stat_time>='"+sStart_yq+"'","")} +--${IF(sPeriod=2,"AND stat_time<='"+sEnd_yq+"'","")} +--${IF(sPeriod=3,"AND stat_time>='"+sStart_yy+"'","")} +--${IF(sPeriod=3,"AND stat_time<='"+sEnd_yy+"'","")} +--GROUP BY +--stat_time, +--bu_name, +--fist_item, +--cost_item, +--pay_way + +--UNION ALL + + select + ${IF(sPeriod=1,"to_char(stat_date,'yyyy-MM') AS stat_time,","")} + ${IF(sPeriod=2,"CASE WHEN MONTH(stat_date)<=3 THEN concat(left(stat_date,4),'-1') + WHEN MONTH(stat_date)<=6 AND MONTH(stat_date)>=4 THEN concat(left(stat_date,4),'-2') + WHEN MONTH(stat_date)<=9 AND MONTH(stat_date)>=7 THEN concat(left(stat_date,4),'-3') + WHEN MONTH(stat_date)<=12 AND MONTH(stat_date)>=10 THEN concat(left(stat_date,4),'-4') + ELSE NULL + END AS stat_time,","")} + ${IF(sPeriod=3,"to_char(stat_date,'yyyy') AS stat_time,","")} + bu_name, + fist_item, + cost_item, + pay_way, + 0 as amt_ys, --预算金额 + sum(amt) AS amt_bx --执行金额 + from dm.v_sal_market_indi_month a + where (indi_type='费用核报') + ${IF(LEN(sBu)=0,"","AND bu_code IN ('"+sBu+"')")} + ${IF(LEN(sRegion)=0,"","AND region_code IN ('"+sRegion+"')")} +AND to_char(stat_date,'yyyy-MM')>='${sStart_mon}' +AND to_char(stat_date,'yyyy-MM')<='${sEnd_mon}' +AND EXISTS ( +SELECT + 1 +FROM + dm.dm_bi_user_permisson +WHERE + bi_user = '${fine_username}' -- 替换为fine_username + AND per_model IN ('all','sal','fim') + 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 + ) +) + GROUP BY + stat_time, + bu_name, + fist_item, + cost_item, + pay_way +) +select + stat_time, + bu_name, + fist_item, + cost_item, + pay_way, + sum(amt_ys) as sum_amt_ys, --预算金额 + sum(amt_bx) AS sum_amt_bx --执行金额 +from tb +GROUP BY + stat_time, + bu_name, + fist_item, + cost_item, + pay_way + + +-- ====================================== +-- chart_市场费用_费效比事业部 +-- ====================================== + + +-- 事业部层级取数 +${if(sLevel=1,"","/*")} +with dda as ( + select deal_code, + bu_code, + max(city_unit_code) as city_unit_code, + max(deal_type) as deal_type, + max(deal_star) as deal_star, + min(is_new) as is_new, + max(market_type) as market_type + from dwr.dim_dealer_administer + where enabled_status='1' + and bu_code!='0202/0209' + and city_unit_code!='0202/02123801001' + group by deal_code,bu_code +),cop as ( + select + nvl(dm.fa_code,a.deal_code) as deal_code, + a.bu_code, + case when '在合作'=any(array_agg(distinct a.deal_status)) + then '有效经销商' + when '无往来'=any(array_agg(distinct a.deal_status)) + then '休眠经销商' + when '待终止'=any(array_agg(distinct a.deal_status)) + then '待终止经销商' + when '已终止'=any(array_agg(distinct a.deal_status)) + then '已终止经销商' + end as deal_status + from dm.dm_cus_coop_info a + left join sdi_hzg.dealer_mapping dm on a.deal_code = dm.code + and a.bu_code = decode(dm.bu_code,'BU_ALL',a.bu_code,dm.bu_code) + and dm.data_status = 1 + where exists ( + select 1 from ( + select max(stat_date) as stat_date from dm.dm_cus_coop_info + where to_char(stat_date,'yyyy')='${YEAR(sEnd_mon)}' + group by to_char(stat_date,'yyyy') + ) c where a.stat_date=c.stat_date + ) + group by nvl(dm.fa_code,a.deal_code), + a.bu_code +),tmp as ( + select + a.indi_type, + ${IF(sPeriod=1,"to_char(stat_date,'yyyy-MM') AS stat_time,","")} + ${IF(sPeriod=2,"CASE WHEN MONTH(stat_date)<=3 THEN concat(left(stat_date,4),'-1') + WHEN MONTH(stat_date)<=6 AND MONTH(stat_date)>=4 THEN concat(left(stat_date,4),'-2') + WHEN MONTH(stat_date)<=9 AND MONTH(stat_date)>=7 THEN concat(left(stat_date,4),'-3') + WHEN MONTH(stat_date)<=12 AND MONTH(stat_date)>=10 THEN concat(left(stat_date,4),'-4') + ELSE NULL + END AS stat_time,","")} + ${IF(sPeriod=3,"to_char(stat_date,'yyyy') AS stat_time,","")} + decode(a.deal_code,'555555',a.src_deal_code,a.deal_code) as deal_code, + a.bu_code, + replace(dep.dept_name,'事业部','') as bu_name, + max(a.city_unit_code) as city_unit_code, + sum(amt_dealer)/10000 as amt + from dm.dm_sal_indicator_day a + left join dwr.dim_department dep on a.bu_code=dep.dept_code + where indi_type ='回款' + and stat_date>=date'2024-01-01' + and a.src_deal_code not in('271827','270335','618241') + ${IF(LEN(sBu)=0,"","AND bu_code IN ('"+sBu+"')")} + AND to_char(stat_date,'yyyy-MM')>='${sStart_mon}' +AND to_char(stat_date,'yyyy-MM')<='${sEnd_mon}' +AND EXISTS ( +SELECT + 1 +FROM + dm.dm_bi_user_permisson +WHERE + bi_user = '${fine_username}' -- 替换为fine_username + AND per_model IN ('all','sal','fim') + 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 + ) +) + group by a.indi_type, + stat_time, + decode(a.deal_code,'555555',a.src_deal_code,a.deal_code), + a.bu_code, + replace(dep.dept_name,'事业部','') + union all + SELECT + '报销' as indi_type, + ${IF(sPeriod=1,"to_char(stat_date,'yyyy-MM') AS stat_time,","")} + ${IF(sPeriod=2,"CASE WHEN MONTH(stat_date)<=3 THEN concat(left(stat_date,4),'-1') +WHEN MONTH(stat_date)<=6 AND MONTH(stat_date)>=4 THEN concat(left(stat_date,4),'-2') +WHEN MONTH(stat_date)<=9 AND MONTH(stat_date)>=7 THEN concat(left(stat_date,4),'-3') +WHEN MONTH(stat_date)<=12 AND MONTH(stat_date)>=10 THEN concat(left(stat_date,4),'-4') +ELSE NULL +END AS stat_time,","")} + ${IF(sPeriod=3,"to_char(stat_date,'yyyy') AS stat_time,","")} + nvl(d.deal_code,a.office_name) as deal_code, + a.bu_code, + replace(dep.dept_name,'事业部','') as bu_name, + max(case when a.city_unit_code in ('0202/021244','0202/021228','0202/021230','0202/021229', + '0202/0212','0202/021240','0202/02123801001') + then '0202/0212000101' + when a.city_unit_code in ('0202/022502','0202/022503','0202/022504','0202/022511','0202/0225') + then '0202/0225090101' + when a.city_unit_code in ('0202/021508','0202/021509','0202/021513','0202/022605', + '0202/022602','0202/022603','0202/022604','0202/021626', + '0202/021627','0202/021624','0202/0215','0202/0216','0202/0226') + then '0202/0226110101' + else a.city_unit_code end) as city_unit_code, + sum(a.amt) as amt + FROM dm.v_sal_market_indi_month a + LEFT JOIN dwr.dim_department dep ON a.bu_code= dep.dept_code + left join sdi_hzg.dealer_mapping dm on a.deal_code=dm.code + and a.bu_code = decode(dm.bu_code,'BU_ALL',a.bu_code,dm.bu_code) + and dm.data_status=1 + and dm.fa_code!='555555' + left join dwr.dim_dealer d on coalesce(dm.fa_code,a.deal_code)=d.deal_code + WHERE a.indi_type = '费用核报' + and to_char(a.stat_date,'yyyy')>='2024' + AND to_char(stat_date,'yyyy-MM')>='${sStart_mon}' + AND to_char(stat_date,'yyyy-MM')<='${sEnd_mon}' + AND EXISTS ( + SELECT + 1 + FROM + dm.dm_bi_user_permisson + WHERE + bi_user = '${fine_username}' -- 替换为fine_username + AND per_model IN ('all','sal','fim') + 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 + ) + ) + group by + stat_time, + nvl(d.deal_code,a.office_name), + a.bu_code, + replace(dep.dept_name,'事业部','') +) + ,deal AS ( + select indi_type, + d.deal_code, + COALESCE(d.deal_name,tmp.deal_code,dd.thrd_dept_name) as deal_name, + dd.fist_dept_code as bu_code, + tmp.bu_name, + amt + from tmp + left join dda on tmp.deal_code=dda.deal_code + and tmp.bu_code=dda.bu_code + left join cop on tmp.deal_code=cop.deal_code + and tmp.bu_code=cop.bu_code + left join dwr.dim_dealer d on tmp.deal_code=d.deal_code + left join dwr.dim_department dd on coalesce(dda.city_unit_code,tmp.city_unit_code)=dd.dept_code +) + ,tb_sum AS + (SELECT replace(bu_name,'推进部','') as bu_name, + sum(amt) as amt_hk, + 0 AS amt_bx + from deal a + where 1=1 + AND bu_name!='公司总部' + and indi_type='回款' + ${if(len(sBu)=0,"","AND a.bu_code IN ('"+sBu+"')")} + group by + bu_name + + UNION ALL + + SELECT replace(bu_name,'推进部','') as bu_name, + 0 AS amt_hk, + sum(amt) as amt_bx + from deal a + where 1=1 + and bu_name!='公司总部' + and indi_type='报销' + ${if(len(sBu)=0,"","AND a.bu_code IN ('"+sBu+"')")} + group by + bu_name + ) +select bu_name,sum(amt_hk) AS amt_huik,sum(amt_bx) AS amt_baox,sum(amt_bx)/sum(amt_hk) AS rate from tb_sum +group by bu_name + + +${if(sLevel=1,"","*/")} + + +-- 事业部大区层级 +${if(sLevel=2,"","/*")} +with dda as ( + select deal_code, + bu_code, + max(city_unit_code) as city_unit_code, + max(deal_type) as deal_type, + max(deal_star) as deal_star, + min(is_new) as is_new, + max(market_type) as market_type + from dwr.dim_dealer_administer + where enabled_status='1' + and bu_code!='0202/0209' + and city_unit_code!='0202/02123801001' + group by deal_code,bu_code +),cop as ( + select + nvl(dm.fa_code,a.deal_code) as deal_code, + a.bu_code, + case when '在合作'=any(array_agg(distinct a.deal_status)) + then '有效经销商' + when '无往来'=any(array_agg(distinct a.deal_status)) + then '休眠经销商' + when '待终止'=any(array_agg(distinct a.deal_status)) + then '待终止经销商' + when '已终止'=any(array_agg(distinct a.deal_status)) + then '已终止经销商' + end as deal_status + from dm.dm_cus_coop_info a + left join sdi_hzg.dealer_mapping dm on a.deal_code = dm.code + and a.bu_code = decode(dm.bu_code,'BU_ALL',a.bu_code,dm.bu_code) + and dm.data_status = 1 + where exists ( + select 1 from ( + select max(stat_date) as stat_date from dm.dm_cus_coop_info + where to_char(stat_date,'yyyy')='${YEAR(sEnd_mon)}' + group by to_char(stat_date,'yyyy') + ) c where a.stat_date=c.stat_date + ) + group by nvl(dm.fa_code,a.deal_code), + a.bu_code +),tmp as ( + select + a.indi_type, + ${IF(sPeriod=1,"to_char(stat_date,'yyyy-MM') AS stat_time,","")} + ${IF(sPeriod=2,"CASE WHEN MONTH(stat_date)<=3 THEN concat(left(stat_date,4),'-1') + WHEN MONTH(stat_date)<=6 AND MONTH(stat_date)>=4 THEN concat(left(stat_date,4),'-2') + WHEN MONTH(stat_date)<=9 AND MONTH(stat_date)>=7 THEN concat(left(stat_date,4),'-3') + WHEN MONTH(stat_date)<=12 AND MONTH(stat_date)>=10 THEN concat(left(stat_date,4),'-4') + ELSE NULL + END AS stat_time,","")} + ${IF(sPeriod=3,"to_char(stat_date,'yyyy') AS stat_time,","")} + decode(a.deal_code,'555555',a.src_deal_code,a.deal_code) as deal_code, + a.bu_code, + replace(dep.dept_name,'事业部','') as bu_name, + dep1.dept_name AS region_name, + max(a.city_unit_code) as city_unit_code, + sum(amt_dealer)/10000 as amt + from dm.dm_sal_indicator_day a + left join dwr.dim_department dep on a.bu_code=dep.dept_code + left join dwr.dim_department dep1 on a.region_code=dep1.dept_code + where indi_type ='回款' + and stat_date>=date'2024-01-01' + and a.src_deal_code not in('271827','270335','618241') + ${IF(LEN(sBu)=0,"","AND bu_code IN ('"+sBu+"')")} + AND to_char(stat_date,'yyyy-MM')>='${sStart_mon}' +AND to_char(stat_date,'yyyy-MM')<='${sEnd_mon}' +AND EXISTS ( +SELECT + 1 +FROM + dm.dm_bi_user_permisson +WHERE + bi_user = '${fine_username}' -- 替换为fine_username + AND per_model IN ('all','sal','fim') + 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 + ) +) + group by a.indi_type, + stat_time, + decode(a.deal_code,'555555',a.src_deal_code,a.deal_code), + a.bu_code, + replace(dep.dept_name,'事业部',''), dep1.dept_name + union all + SELECT + '报销' as indi_type, + ${IF(sPeriod=1,"to_char(stat_date,'yyyy-MM') AS stat_time,","")} + ${IF(sPeriod=2,"CASE WHEN MONTH(stat_date)<=3 THEN concat(left(stat_date,4),'-1') +WHEN MONTH(stat_date)<=6 AND MONTH(stat_date)>=4 THEN concat(left(stat_date,4),'-2') +WHEN MONTH(stat_date)<=9 AND MONTH(stat_date)>=7 THEN concat(left(stat_date,4),'-3') +WHEN MONTH(stat_date)<=12 AND MONTH(stat_date)>=10 THEN concat(left(stat_date,4),'-4') +ELSE NULL +END AS stat_time,","")} + ${IF(sPeriod=3,"to_char(stat_date,'yyyy') AS stat_time,","")} + nvl(d.deal_code,a.office_name) as deal_code, + a.bu_code, + replace(dep.dept_name,'事业部','') as bu_name, + dep1.dept_name AS region_name, + max(case when a.city_unit_code in ('0202/021244','0202/021228','0202/021230','0202/021229', + '0202/0212','0202/021240','0202/02123801001') + then '0202/0212000101' + when a.city_unit_code in ('0202/022502','0202/022503','0202/022504','0202/022511','0202/0225') + then '0202/0225090101' + when a.city_unit_code in ('0202/021508','0202/021509','0202/021513','0202/022605', + '0202/022602','0202/022603','0202/022604','0202/021626', + '0202/021627','0202/021624','0202/0215','0202/0216','0202/0226') + then '0202/0226110101' + else a.city_unit_code end) as city_unit_code, + sum(a.amt) as amt + FROM dm.v_sal_market_indi_month a + LEFT JOIN dwr.dim_department dep ON a.bu_code= dep.dept_code + left join dwr.dim_department dep1 on a.region_code=dep1.dept_code + left join sdi_hzg.dealer_mapping dm on a.deal_code=dm.code + and a.bu_code = decode(dm.bu_code,'BU_ALL',a.bu_code,dm.bu_code) + and dm.data_status=1 + and dm.fa_code!='555555' + left join dwr.dim_dealer d on coalesce(dm.fa_code,a.deal_code)=d.deal_code + WHERE a.indi_type = '费用核报' + and to_char(a.stat_date,'yyyy')>='2024' + AND to_char(stat_date,'yyyy-MM')>='${sStart_mon}' + AND to_char(stat_date,'yyyy-MM')<='${sEnd_mon}' + AND EXISTS ( + SELECT + 1 + FROM + dm.dm_bi_user_permisson + WHERE + bi_user = '${fine_username}' -- 替换为fine_username + AND per_model IN ('all','sal','fim') + 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 + ) + ) + group by + stat_time, + nvl(d.deal_code,a.office_name), + a.bu_code, + replace(dep.dept_name,'事业部',''), dep1.dept_name +) + ,deal AS ( + select indi_type, + d.deal_code, + COALESCE(d.deal_name,tmp.deal_code,dd.thrd_dept_name) as deal_name, + dd.fist_dept_code as bu_code, + tmp.bu_name, + tmp.region_name, + amt + from tmp + left join dda on tmp.deal_code=dda.deal_code + and tmp.bu_code=dda.bu_code + left join cop on tmp.deal_code=cop.deal_code + and tmp.bu_code=cop.bu_code + left join dwr.dim_dealer d on tmp.deal_code=d.deal_code + left join dwr.dim_department dd on coalesce(dda.city_unit_code,tmp.city_unit_code)=dd.dept_code +) + ,tb_sum AS + (SELECT region_name as bu_name, + sum(amt) as amt_hk, + 0 AS amt_bx + from deal a + where 1=1 + AND bu_name!='公司总部' + and indi_type='回款' + ${if(len(sBu)=0,"","AND a.bu_code IN ('"+sBu+"')")} + group by + region_name + + UNION ALL + + SELECT region_name as bu_name, + 0 AS amt_hk, + sum(amt) as amt_bx + from deal a + where 1=1 + and bu_name!='公司总部' + and indi_type='报销' + ${if(len(sBu)=0,"","AND a.bu_code IN ('"+sBu+"')")} + group by + region_name + ) +select + bu_name + ,sum(amt_hk) AS amt_huik + ,sum(amt_bx) AS amt_baox + ,CASE WHEN sum(amt_hk) = 0 THEN 0 ELSE sum(amt_bx)/sum(amt_hk) END AS rate +from tb_sum +group by bu_name + + +${if(sLevel=2,"","*/")} + + +-- ====================================== +-- chart_市场费用_费效比经销商 +-- ====================================== + +with dda as ( + select deal_code, + bu_code, + max(city_unit_code) as city_unit_code, + max(deal_type) as deal_type, + max(deal_star) as deal_star, + min(is_new) as is_new, + max(market_type) as market_type + from dwr.dim_dealer_administer + where enabled_status='1' + and bu_code!='0202/0209' + and city_unit_code!='0202/02123801001' + group by deal_code,bu_code +),cop as ( + select + nvl(dm.fa_code,a.deal_code) as deal_code, + a.bu_code, + case when '在合作'=any(array_agg(distinct a.deal_status)) + then '有效经销商' + when '无往来'=any(array_agg(distinct a.deal_status)) + then '休眠经销商' + when '待终止'=any(array_agg(distinct a.deal_status)) + then '待终止经销商' + when '已终止'=any(array_agg(distinct a.deal_status)) + then '已终止经销商' + end as deal_status + from dm.dm_cus_coop_info a + left join sdi_hzg.dealer_mapping dm on a.deal_code = dm.code + and a.bu_code = decode(dm.bu_code,'BU_ALL',a.bu_code,dm.bu_code) + and dm.data_status = 1 + where exists ( + select 1 from ( + select max(stat_date) as stat_date from dm.dm_cus_coop_info + where to_char(stat_date,'yyyy')='${YEAR(sEnd_mon)}' + group by to_char(stat_date,'yyyy') + ) c where a.stat_date=c.stat_date + ) + group by nvl(dm.fa_code,a.deal_code), + a.bu_code +),tmp as ( + select + a.indi_type, + ${IF(sPeriod=1,"to_char(stat_date,'yyyy-MM') AS stat_time,","")} + ${IF(sPeriod=2,"CASE WHEN MONTH(stat_date)<=3 THEN concat(left(stat_date,4),'-1') + WHEN MONTH(stat_date)<=6 AND MONTH(stat_date)>=4 THEN concat(left(stat_date,4),'-2') + WHEN MONTH(stat_date)<=9 AND MONTH(stat_date)>=7 THEN concat(left(stat_date,4),'-3') + WHEN MONTH(stat_date)<=12 AND MONTH(stat_date)>=10 THEN concat(left(stat_date,4),'-4') + ELSE NULL + END AS stat_time,","")} + ${IF(sPeriod=3,"to_char(stat_date,'yyyy') AS stat_time,","")} + decode(a.deal_code,'555555',a.src_deal_code,a.deal_code) as deal_code, + a.bu_code, + replace(dep.dept_name,'事业部','') as bu_name, + max(a.city_unit_code) as city_unit_code, + sum(amt_dealer)/10000 as amt + from dm.dm_sal_indicator_day a + left join dwr.dim_department dep on a.bu_code=dep.dept_code + where indi_type ='回款' + and stat_date>=date'2024-01-01' + and a.src_deal_code not in('271827','270335','618241') + ${IF(LEN(sBu)=0,"","AND bu_code IN ('"+sBu+"')")} + ${IF(LEN(sRegion)=0,"","AND region_code IN ('"+sRegion+"')")} + AND to_char(stat_date,'yyyy-MM')>='${sStart_mon}' +AND to_char(stat_date,'yyyy-MM')<='${sEnd_mon}' +AND EXISTS ( +SELECT + 1 +FROM + dm.dm_bi_user_permisson +WHERE + bi_user = '${fine_username}' -- 替换为fine_username + AND per_model IN ('all','sal','fim') + 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 + ) +) + group by a.indi_type, + stat_time, + decode(a.deal_code,'555555',a.src_deal_code,a.deal_code), + a.bu_code, + replace(dep.dept_name,'事业部','') + union all + SELECT + '报销' as indi_type, + ${IF(sPeriod=1,"to_char(stat_date,'yyyy-MM') AS stat_time,","")} + ${IF(sPeriod=2,"CASE WHEN MONTH(stat_date)<=3 THEN concat(left(stat_date,4),'-1') +WHEN MONTH(stat_date)<=6 AND MONTH(stat_date)>=4 THEN concat(left(stat_date,4),'-2') +WHEN MONTH(stat_date)<=9 AND MONTH(stat_date)>=7 THEN concat(left(stat_date,4),'-3') +WHEN MONTH(stat_date)<=12 AND MONTH(stat_date)>=10 THEN concat(left(stat_date,4),'-4') +ELSE NULL +END AS stat_time,","")} + ${IF(sPeriod=3,"to_char(stat_date,'yyyy') AS stat_time,","")} + nvl(d.deal_code,a.office_name) as deal_code, + a.bu_code, + replace(dep.dept_name,'事业部','') as bu_name, + max(case when a.city_unit_code in ('0202/021244','0202/021228','0202/021230','0202/021229', + '0202/0212','0202/021240','0202/02123801001') + then '0202/0212000101' + when a.city_unit_code in ('0202/022502','0202/022503','0202/022504','0202/022511','0202/0225') + then '0202/0225090101' + when a.city_unit_code in ('0202/021508','0202/021509','0202/021513','0202/022605', + '0202/022602','0202/022603','0202/022604','0202/021626', + '0202/021627','0202/021624','0202/0215','0202/0216','0202/0226') + then '0202/0226110101' + else a.city_unit_code end) as city_unit_code, + sum(a.amt) as amt + FROM dm.v_sal_market_indi_month a + LEFT JOIN dwr.dim_department dep ON a.bu_code= dep.dept_code + left join sdi_hzg.dealer_mapping dm on a.deal_code=dm.code + and a.bu_code = decode(dm.bu_code,'BU_ALL',a.bu_code,dm.bu_code) + and dm.data_status=1 + and dm.fa_code!='555555' + left join dwr.dim_dealer d on coalesce(dm.fa_code,a.deal_code)=d.deal_code + WHERE a.indi_type = '费用核报' + and to_char(a.stat_date,'yyyy')>='2024' + AND to_char(stat_date,'yyyy-MM')>='${sStart_mon}' + AND to_char(stat_date,'yyyy-MM')<='${sEnd_mon}' + ${IF(LEN(sRegion)=0,"","AND region_code IN ('"+sRegion+"')")} + AND EXISTS ( + SELECT + 1 + FROM + dm.dm_bi_user_permisson + WHERE + bi_user = '${fine_username}' -- 替换为fine_username + AND per_model IN ('all','sal','fim') + 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 + ) + ) + group by + stat_time, + nvl(d.deal_code,a.office_name), + a.bu_code, + replace(dep.dept_name,'事业部','') +) + ,deal AS ( + select indi_type, + d.deal_code, + COALESCE(d.deal_name,tmp.deal_code,dd.thrd_dept_name) as deal_name, + dd.fist_dept_code as bu_code, + tmp.bu_name, + amt + from tmp + left join dda on tmp.deal_code=dda.deal_code + and tmp.bu_code=dda.bu_code + left join cop on tmp.deal_code=cop.deal_code + and tmp.bu_code=cop.bu_code + left join dwr.dim_dealer d on tmp.deal_code=d.deal_code + left join dwr.dim_department dd on coalesce(dda.city_unit_code,tmp.city_unit_code)=dd.dept_code +) + ,tb_sum AS + (SELECT deal_name, + sum(amt) as amt_hk, + 0 AS amt_bx + from deal a + where 1=1 + AND bu_name!='公司总部' + and indi_type='回款' + ${if(len(sBu)=0,"","AND a.bu_code IN ('"+sBu+"')")} + group by + deal_name + + UNION ALL + + SELECT deal_name, + 0 AS amt_hk, + sum(amt) as amt_bx + from deal a + where 1=1 +--and bu_name!='公司总部' + and indi_type='报销' + ${if(len(sBu)=0,"","AND a.bu_code IN ('"+sBu+"')")} + group by + deal_name + ) + ,tb_sum2 AS (select deal_name,sum(amt_hk) AS amt_huik,sum(amt_bx) AS amt_baox from tb_sum + group by deal_name +) +select *, if(amt_huik=0,0,amt_baox/amt_huik) AS rate from tb_sum2 +--where amt_huik!=0 and amt_huik>1 +order by rate desc + + +-- ====================================== +-- chart_市场费用_费效比趋势 +-- ====================================== + + +with dda as ( + select deal_code, + bu_code, + max(city_unit_code) as city_unit_code, + max(deal_type) as deal_type, + max(deal_star) as deal_star, + min(is_new) as is_new, + max(market_type) as market_type + from dwr.dim_dealer_administer + where enabled_status='1' + and bu_code!='0202/0209' + and city_unit_code!='0202/02123801001' + group by deal_code,bu_code +),cop as ( + select + nvl(dm.fa_code,a.deal_code) as deal_code, + a.bu_code, + case when '在合作'=any(array_agg(distinct a.deal_status)) + then '有效经销商' + when '无往来'=any(array_agg(distinct a.deal_status)) + then '休眠经销商' + when '待终止'=any(array_agg(distinct a.deal_status)) + then '待终止经销商' + when '已终止'=any(array_agg(distinct a.deal_status)) + then '已终止经销商' + end as deal_status + from dm.dm_cus_coop_info a + left join sdi_hzg.dealer_mapping dm on a.deal_code = dm.code + and a.bu_code = decode(dm.bu_code,'BU_ALL',a.bu_code,dm.bu_code) + and dm.data_status = 1 + where exists ( + select 1 from ( + select max(stat_date) as stat_date from dm.dm_cus_coop_info + where to_char(stat_date,'yyyy')='${YEAR(sEnd_mon)}' + group by to_char(stat_date,'yyyy') + ) c where a.stat_date=c.stat_date + ) + group by nvl(dm.fa_code,a.deal_code), + a.bu_code +),tmp as ( + select + a.indi_type, + ${IF(sPeriod=1,"to_char(stat_date,'yyyy-MM') AS stat_time,","")} + ${IF(sPeriod=2,"CASE WHEN MONTH(stat_date)<=3 THEN concat(left(stat_date,4),'-1') + WHEN MONTH(stat_date)<=6 AND MONTH(stat_date)>=4 THEN concat(left(stat_date,4),'-2') + WHEN MONTH(stat_date)<=9 AND MONTH(stat_date)>=7 THEN concat(left(stat_date,4),'-3') + WHEN MONTH(stat_date)<=12 AND MONTH(stat_date)>=10 THEN concat(left(stat_date,4),'-4') + ELSE NULL + END AS stat_time,","")} + ${IF(sPeriod=3,"to_char(stat_date,'yyyy') AS stat_time,","")} + decode(a.deal_code,'555555',a.src_deal_code,a.deal_code) as deal_code, + a.bu_code, + replace(dep.dept_name,'事业部','') as bu_name, + max(a.city_unit_code) as city_unit_code, + sum(amt_dealer)/10000 as amt + from dm.dm_sal_indicator_day a + left join dwr.dim_department dep on a.bu_code=dep.dept_code + where indi_type ='回款' + and stat_date>=date'2024-01-01' + and a.src_deal_code not in('271827','270335','618241') + ${IF(LEN(sBu)=0,"","AND bu_code IN ('"+sBu+"')")} + ${IF(LEN(sRegion)=0,"","AND region_code IN ('"+sRegion+"')")} + AND to_char(stat_date,'yyyy-MM')>='${sStart_mon}' +AND to_char(stat_date,'yyyy-MM')<='${sEnd_mon}' +AND EXISTS ( +SELECT + 1 +FROM + dm.dm_bi_user_permisson +WHERE + bi_user = '${fine_username}' -- 替换为fine_username + AND per_model IN ('all','sal','fim') + 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 + ) +) + group by a.indi_type, + stat_time, + decode(a.deal_code,'555555',a.src_deal_code,a.deal_code), + a.bu_code, + replace(dep.dept_name,'事业部','') + union all + SELECT + '报销' as indi_type, + ${IF(sPeriod=1,"to_char(stat_date,'yyyy-MM') AS stat_time,","")} + ${IF(sPeriod=2,"CASE WHEN MONTH(stat_date)<=3 THEN concat(left(stat_date,4),'-1') +WHEN MONTH(stat_date)<=6 AND MONTH(stat_date)>=4 THEN concat(left(stat_date,4),'-2') +WHEN MONTH(stat_date)<=9 AND MONTH(stat_date)>=7 THEN concat(left(stat_date,4),'-3') +WHEN MONTH(stat_date)<=12 AND MONTH(stat_date)>=10 THEN concat(left(stat_date,4),'-4') +ELSE NULL +END AS stat_time,","")} + ${IF(sPeriod=3,"to_char(stat_date,'yyyy') AS stat_time,","")} + nvl(d.deal_code,a.office_name) as deal_code, + a.bu_code, + replace(dep.dept_name,'事业部','') as bu_name, + max(case when a.city_unit_code in ('0202/021244','0202/021228','0202/021230','0202/021229', + '0202/0212','0202/021240','0202/02123801001') + then '0202/0212000101' + when a.city_unit_code in ('0202/022502','0202/022503','0202/022504','0202/022511','0202/0225') + then '0202/0225090101' + when a.city_unit_code in ('0202/021508','0202/021509','0202/021513','0202/022605', + '0202/022602','0202/022603','0202/022604','0202/021626', + '0202/021627','0202/021624','0202/0215','0202/0216','0202/0226') + then '0202/0226110101' + else a.city_unit_code end) as city_unit_code, + sum(a.amt) as amt + FROM dm.v_sal_market_indi_month a + LEFT JOIN dwr.dim_department dep ON a.bu_code= dep.dept_code + left join sdi_hzg.dealer_mapping dm on a.deal_code=dm.code + and a.bu_code = decode(dm.bu_code,'BU_ALL',a.bu_code,dm.bu_code) + and dm.data_status=1 + and dm.fa_code!='555555' + left join dwr.dim_dealer d on coalesce(dm.fa_code,a.deal_code)=d.deal_code + WHERE a.indi_type = '费用核报' + and to_char(a.stat_date,'yyyy')>='2024' + AND to_char(stat_date,'yyyy-MM')>='${sStart_mon}' + AND to_char(stat_date,'yyyy-MM')<='${sEnd_mon}' + ${IF(LEN(sRegion)=0,"","AND region_code IN ('"+sRegion+"')")} + AND EXISTS ( + SELECT + 1 + FROM + dm.dm_bi_user_permisson + WHERE + bi_user = '${fine_username}' -- 替换为fine_username + AND per_model IN ('all','sal','fim') + 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 + ) + ) + group by + stat_time, + nvl(d.deal_code,a.office_name), + a.bu_code, + replace(dep.dept_name,'事业部','') +) + ,deal AS ( + select indi_type, + stat_time, + d.deal_code, + COALESCE(d.deal_name,tmp.deal_code,dd.thrd_dept_name) as deal_name, + dd.fist_dept_code as bu_code, + tmp.bu_name, + amt + from tmp + left join dda on tmp.deal_code=dda.deal_code + and tmp.bu_code=dda.bu_code + left join cop on tmp.deal_code=cop.deal_code + and tmp.bu_code=cop.bu_code + left join dwr.dim_dealer d on tmp.deal_code=d.deal_code + left join dwr.dim_department dd on coalesce(dda.city_unit_code,tmp.city_unit_code)=dd.dept_code +) + ,tb_sum AS + (SELECT stat_time, + sum(amt) as amt_hk, + 0 AS amt_bx + from deal a + where 1=1 + AND bu_name!='公司总部' + and indi_type='回款' + ${if(len(sBu)=0,"","AND a.bu_code IN ('"+sBu+"')")} + group by + stat_time + + UNION ALL + + SELECT stat_time, + 0 AS amt_hk, + sum(amt) as amt_bx + from deal a + where 1=1 + and bu_name!='公司总部' + and indi_type='报销' + ${if(len(sBu)=0,"","AND a.bu_code IN ('"+sBu+"')")} + group by + stat_time + ) + ,tb_sum2 AS (select stat_time,sum(amt_hk) AS amt_huik,sum(amt_bx) AS amt_baox from tb_sum + group by stat_time +) +select + * + ,CASE WHEN amt_huik = 0 THEN 0 ELSE amt_baox/amt_huik END AS rate from tb_sum2 +where amt_huik!=0 and amt_huik>1 +order by stat_time + + + +-- ====================================== +-- report_市场费用_事业部大区预算执行 +-- ====================================== + +-- 事业部 + +${if(sLevel=1,"","/*")} + + + +-- 费用执行 +select + + ${IF(sPeriod=1,"to_char(stat_date,'yyyy-MM') AS stat_time,","")} + ${IF(sPeriod=2,"CASE WHEN MONTH(stat_date)<=3 THEN concat(left(stat_date,4),'-1') + WHEN MONTH(stat_date)<=6 AND MONTH(stat_date)>=4 THEN concat(left(stat_date,4),'-2') + WHEN MONTH(stat_date)<=9 AND MONTH(stat_date)>=7 THEN concat(left(stat_date,4),'-3') + WHEN MONTH(stat_date)<=12 AND MONTH(stat_date)>=10 THEN concat(left(stat_date,4),'-4') + ELSE NULL + END AS stat_time,","")} + ${IF(sPeriod=3,"to_char(stat_date,'yyyy') AS stat_time,","")} + bu_name, + sum(amt) AS amt_ex, + 0 AS amt_bug -- 预算 +from dm.v_sal_market_indi_month a +where (indi_type='费用核报') + ${IF(LEN(sBu)=0,"","AND bu_code IN ('"+sBu+"')")} + ${IF(LEN(sRegion)=0,"","AND region_code IN ('"+sRegion+"')")} + AND to_char(stat_date,'yyyy-MM')>='${sStart_mon}' + AND to_char(stat_date,'yyyy-MM')<='${sEnd_mon}' + AND EXISTS ( + SELECT + 1 + FROM + dm.dm_bi_user_permisson + WHERE + bi_user = '${fine_username}' -- 替换为fine_username + AND per_model IN ('all','sal','fim') + 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 + ) + ) +GROUP BY + stat_time, + bu_name +UNION all +-- 费用预算 +select + ${IF(sPeriod=1,"to_char(stat_date,'yyyy-MM') AS stat_time,","")} + ${IF(sPeriod=2,"CASE WHEN MONTH(stat_date)<=3 THEN concat(left(stat_date,4),'-1') + WHEN MONTH(stat_date)<=6 AND MONTH(stat_date)>=4 THEN concat(left(stat_date,4),'-2') + WHEN MONTH(stat_date)<=9 AND MONTH(stat_date)>=7 THEN concat(left(stat_date,4),'-3') + WHEN MONTH(stat_date)<=12 AND MONTH(stat_date)>=10 THEN concat(left(stat_date,4),'-4') + ELSE NULL + END AS stat_time,","")} + ${IF(sPeriod=3,"to_char(stat_date,'yyyy') AS stat_time,","")} + bu_name, + 0 AS amt_ex, + sum(amt) AS amt_bug +from dm.v_sal_market_indi_month a +where (indi_type='期初预算' OR indi_type='调整预算') + ${IF(LEN(sBu)=0,"","AND bu_code IN ('"+sBu+"')")} + ${IF(LEN(sRegion)=0,"","AND region_code IN ('"+sRegion+"')")} + AND to_char(stat_date,'yyyy')='${YEAR(sEnd_mon)}' + AND EXISTS ( + SELECT + 1 + FROM + dm.dm_bi_user_permisson + WHERE + bi_user = '${fine_username}' -- 替换为fine_username + AND per_model IN ('all','sal','fim') + 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 + ) + ) +GROUP BY + stat_time, + bu_name + +${if(sLevel=1,"","*/")} + + + +-- 事业部大区 +${if(sLevel=2,"","/*")} + + +-- 费用执行 +select + + ${IF(sPeriod=1,"to_char(stat_date,'yyyy-MM') AS stat_time,","")} + ${IF(sPeriod=2,"CASE WHEN MONTH(stat_date)<=3 THEN concat(left(stat_date,4),'-1') + WHEN MONTH(stat_date)<=6 AND MONTH(stat_date)>=4 THEN concat(left(stat_date,4),'-2') + WHEN MONTH(stat_date)<=9 AND MONTH(stat_date)>=7 THEN concat(left(stat_date,4),'-3') + WHEN MONTH(stat_date)<=12 AND MONTH(stat_date)>=10 THEN concat(left(stat_date,4),'-4') + ELSE NULL + END AS stat_time,","")} + ${IF(sPeriod=3,"to_char(stat_date,'yyyy') AS stat_time,","")} + region_name AS bu_name, + sum(amt) AS amt_ex, + 0 AS amt_bug -- 预算 +from dm.v_sal_market_indi_month a +where (indi_type='费用核报') + ${IF(LEN(sBu)=0,"","AND bu_code IN ('"+sBu+"')")} + ${IF(LEN(sRegion)=0,"","AND region_code IN ('"+sRegion+"')")} + AND to_char(stat_date,'yyyy-MM')>='${sStart_mon}' + AND to_char(stat_date,'yyyy-MM')<='${sEnd_mon}' + AND EXISTS ( + SELECT + 1 + FROM + dm.dm_bi_user_permisson + WHERE + bi_user = '${fine_username}' -- 替换为fine_username + AND per_model IN ('all','sal','fim') + 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 + ) + ) +GROUP BY + stat_time, + region_name +UNION all +-- 费用预算 +select + ${IF(sPeriod=1,"to_char(stat_date,'yyyy-MM') AS stat_time,","")} + ${IF(sPeriod=2,"CASE WHEN MONTH(stat_date)<=3 THEN concat(left(stat_date,4),'-1') + WHEN MONTH(stat_date)<=6 AND MONTH(stat_date)>=4 THEN concat(left(stat_date,4),'-2') + WHEN MONTH(stat_date)<=9 AND MONTH(stat_date)>=7 THEN concat(left(stat_date,4),'-3') + WHEN MONTH(stat_date)<=12 AND MONTH(stat_date)>=10 THEN concat(left(stat_date,4),'-4') + ELSE NULL + END AS stat_time,","")} + ${IF(sPeriod=3,"to_char(stat_date,'yyyy') AS stat_time,","")} + region_name bu_name, + 0 AS amt_ex, + sum(amt) AS amt_bug +from dm.v_sal_market_indi_month a +where (indi_type='期初预算' OR indi_type='调整预算') + ${IF(LEN(sBu)=0,"","AND bu_code IN ('"+sBu+"')")} + ${IF(LEN(sRegion)=0,"","AND region_code IN ('"+sRegion+"')")} + AND to_char(stat_date,'yyyy')='${YEAR(sEnd_mon)}' + AND EXISTS ( + SELECT + 1 + FROM + dm.dm_bi_user_permisson + WHERE + bi_user = '${fine_username}' -- 替换为fine_username + AND per_model IN ('all','sal','fim') + 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 + ) + ) +GROUP BY + stat_time, + region_name + +${if(sLevel=2,"","*/")} \ No newline at end of file