IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[report_12_data]') AND type = 'P')
DROP PROCEDURE [dbo].[report_12_data]
GO
CREATE PROCEDURE report_12_data
@cmpid int,@branchid int,@userdpid int,@userotid int,@useruid int,@tableid varchar(8000),@objids varchar(8000),@pagesize int,@qry_mdbh varchar(512)
,@qry_date1 varchar(512)
,@qry_date2 varchar(512)
,@qry_fgs varchar(512)

AS
BEGIN
 set nocount on 

declare @dataIds table(objid int)
if @objids!='_NONE' and @objids!='_none_' and @objids!='' 
insert into @dataIds(objid)
select value from dbo.fn_split_int(@objids)


create table #temp (
    tableid varchar(10),
    ord_branch varchar(100),
    ord_mdbh varchar(100),
    ord_createdepart varchar(100),
    ord_creator varchar(100),
    ord_wlxd int,
    ord_dgj int,
    ord_yyy int,
    ord_jj int,
    ord_count int,
    ord_ydd int,
    ord_ycj int,
    ord_jtl decimal(10,2),
    ord_ddl decimal(10,2),
    ord_gjl decimal(10,2)
)

declare @qry_dpid int
select @qry_dpid = dp_id from mps_depart where dp_code = @qry_mdbh

insert into #temp(tableid,ord_branch,ord_mdbh,ord_createdepart,ord_creator,ord_wlxd,ord_dgj,ord_yyy,ord_jj,ord_count)
select 'tb_report1' tableid, ord_branch,ord_mdbh,ord_createdepart,ord_creator,
        COUNT(case when ord_whzt = '未联系到' then
                 ord_creator
             end) as ord_wlxd,
         COUNT(case when ord_whzt = '待跟进' then
                 ord_creator
             end) as ord_dgj,
         COUNT(case when ord_whzt = '已预约' then
                 ord_creator
             end) as ord_yyy,
         COUNT(case when ord_whzt = '拒绝' then
                 ord_creator
             end) as ord_jj,
         count(ord_creator) ord_count
from order_239
where ord_whsj >= @qry_date1 + ' 00:00:00'
and ord_whsj <= @qry_date2 + ' 23:59:59'
--and (@qry_fgs = '' or ord_branch = @qry_fgs)
and charindex('临',ord_no) = 0 and ord_createdpid = @qry_dpid
group by ord_branch,ord_mdbh,ord_createdepart,ord_creator
order by ord_branch,ord_mdbh

update #temp set ord_ydd = (select count(ord_creator) from order_239
where ord_cjsj >= @qry_date1 + ' 00:00:00'
and ord_cjsj <= @qry_date2 + ' 23:59:59'
and charindex('临',ord_no) = 0 
and #temp.ord_branch = order_239.ord_branch 
and ord_createdpid = @qry_dpid 
and #temp.ord_creator = order_239.ord_creator
and #temp.ord_mdbh = order_239.ord_mdbh
and ord_ddzt = '已到店'
)

update #temp set ord_ycj = (select count(ord_creator) from order_239
where ord_cjsj >= @qry_date1 + ' 00:00:00'
and ord_cjsj <= @qry_date2 + ' 23:59:59'
and charindex('临',ord_no) = 0 
and #temp.ord_branch = order_239.ord_branch 
and ord_createdpid = @qry_dpid
and #temp.ord_creator = order_239.ord_creator
and #temp.ord_mdbh = order_239.ord_mdbh
and ord_sfcj = '已成交'
)

update #temp set ord_jtl = case when ord_count > 0 then (ord_dgj + ord_yyy + ord_jj)/(ord_count + 0.0)*100 else 0 end,
                 ord_ddl = case when ord_yyy > 0 then ord_ydd / (ord_yyy + 0.0)*100 else 0 end,
                 ord_gjl = case when ord_ydd > 0 then ord_ycj / (ord_ydd + 0.0)*100 else 0 end

select * from #temp







END