-- *****************************
-- 创建人员: 杨坤安
-- 创建时间: 202401120
-- 功能描述: 原报表名称:  04城市单元主要指标达成
-- *****************************


-- ======================================
-- 08销售指标达成(看板)
-- ======================================

select a.pk,
       a.stat_date,
       a.indi_type,
       a.bu_code,
       o.dept_name as bu_name,
       a.region_code,
       d.secd_dept_name as region_name,
       a.office_code,
       d.thrd_dept_name as office_name,
       a.city_unit_code,
       d.dept_name as city_unit_name,
       pv.town_name as province_name,
       cit.town_name as city_name,
       a.deal_code,
       deal.deal_name,
       a.src_deal_code,
       a.deal_type,
       a.product_code,
       a.num_bu,
       a.num_region,
       a.num_office,
       a.num_city_unit,
       a.num_dealer,
       a.amt_bu,
       a.amt_region,
       a.amt_office,
       a.amt_city_unit,
       a.amt_dealer,
       a.update_time,
       a.is_new_dealer
from dm.dm_sal_indicator_day a
         left join dwr.dim_department d on d.dept_code=a.city_unit_code
         left join dwr.dim_dealer deal on a.deal_code=deal.deal_code
         left join dwr.dim_department o on o.dept_code=a.bu_code
         left join dwr.dim_product po on a.product_code=po.product_code
         left join (select city_unit_code,
                           max(province_code) as province_code,
                           max(city_code) as city_code
                    from dwr.dim_dealer_administer
                    where enabled_status=1
                    group by city_unit_code) are on a.city_unit_code=are.city_unit_code
         left join dwr.dim_area pv on nvl(a.province_code,are.province_code)=pv.town_code
         left join dwr.dim_area cit on nvl(a.city_code,are.city_code)=cit.town_code
         left join dwr.dim_department dd on (case when
                                                      a.indi_type in ('融资未填仓','已填仓未下单','未下单往来余额','费用余额')
                                                      then a.bu_code else po.bu_code end)=dd.dept_code
where  stat_date>=date'2020-01-01'






-- ======================================
-- 05城市单元销售指标达成
-- ======================================


select
stat_date
,indi_type
,cp_region_code
,cp_region_name
,bu_code
,bu_name
,region_code
,region_name
,office_code
,office_name
,city_unit_code
,city_unit_name
,deal_type
,province
,city
-- 年度
-- 月度
-- 季度
,amt_cityunit -- 金额
from dm.v_sal_indicator_cityunit
