langjiu-project/BI/销售业绩/经销商出货.sql

79 lines
2.5 KiB
SQL

SELECT
*
,CASE WHEN bu_code ='0202/0223'
THEN (CASE WHEN pl_name = '小郎酒' THEN '小郎酒事业部'
WHEN pl_name IN ('郎牌特曲', '郎牌原浆', '新郎酒') THEN '郎牌特曲事业部'
WHEN pl_name in ('青花郎', '老郎酒', '藏品') THEN '青花郎事业部'END)
ELSE bu_name END report_bu_name -- 报表事业部
,CASE WHEN office_name LIKE '%成都%'
THEN '成都市场'
ELSE region_name
END AS report_region_name -- 报表大区
,year(bill_time) AS bill_year
,quarter(bill_time) as bill_quar
,month(bill_time) AS bill_month
,round(num_dealer / pack_base_num,4) AS dealer_num -- 计经销商件数
,round(num_city_unit / pack_base_num,4) AS city_num -- 计业务区件数
FROM dm.v_ag_sal_cust_flow a
-- 出货时间
WHERE 1 =1
-- 事业部
${if(len(sBu) == 0,"","and bu_code in ('" + sBu + "')")}
-- 大区
${if(len(sRegion) == 0,"","and region_code in ('" + sRegion + "')")}
-- 办事处
${if(len(sOffice) == 0,"","and office_code in ('" + sOffice + "')")}
-- 城市单元
${if(len(sCu) == 0,"","and city_unit_code in ('" + sCu + "')")}
-- 省份
${if(len(sProvince) == 0,"","and province_name in ('" + sProvince + "')")}
-- 地市
${if(len(sCity) == 0,"","and city_name in ('" + sCity + "')")}
-- 经销商类型
${if(len(sDeal_type) == 0,"","and deal_type in ('" + sDeal_type + "')")}
-- 产品
${if(len(sProd) == 0,"","and product_code in ('" + sProd + "')")}
-- 品项
${if(len(sPi_name) == 0,"","and pi_name in ('" + sPi_name + "')")}
-- 产品线
${if(len(sPl_name) == 0,"","and pl_name in ('" + sPl_name + "')")}
-- 子主经销商筛选
${if(sSub_main== "主经销商",if(len(sDeal)==0,"","and deal_code in ('" + sDeal + "')"),if(len(sDeal)==0,"","and src_deal_code in ('" +sDeal + "')"))}
-- 收货方
${if(len(sOpposite_name) == 0,"","and opposite_name like ('%" + sOpposite_name + "%')")}
-- 单据号 bill_code
${if(len(sBill) == 0,"","and bill_code in ('" + sBill + "')")}
and bill_time >= date('${sStart_date}')
and bill_time < date('${sEnd_date}') + INTERVAL '1 days'
-- 权限控制
AND exists (
select
1
from
dm.dm_bi_user_permisson
where
bi_user = '${fine_username}'
and per_model IN ('all','sal')
and (
is_all = 1
OR per_code = a.bu_code -- 事业部
OR per_code = a.region_code -- 大区
OR per_code = a.office_code -- 城市单元
OR per_code = a.city_unit_code -- 城市单元
)
)