langjiu-project/FineReport/销售主题/看板/销售管理驾驶舱-市场费用.sql

1803 lines
62 KiB
MySQL
Raw Normal View History

-- ======================================
-- 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,"","*/")}