IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[report_13_data]') AND type = 'P')
DROP PROCEDURE [dbo].[report_13_data]
GO
CREATE PROCEDURE report_13_data
@cmpid int,@branchid int,@userdpid int,@userotid int,@useruid int,@tableid varchar(8000),@objids varchar(8000),@pagesize int
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)
select 'tbcrossid1' tableid, ord_branch , 
case when ord_status='外呼跟进' and ord_whzt='待跟进' then '待跟进'
     when ord_status='预约成功' and ord_whzt='已预约' then '已预约'
     when ord_status='外呼跟进' and ord_whzt='未联系到' then '未联系到'
     when ord_status='外呼跟进' and ord_sfcj='未成交' and ord_ddzt='未到店' then '未到店'
end ord_hmzt,
case when ord_status='外呼跟进' and ord_whzt='待跟进' and DATEDIFF(day,ord_xcgjsj,GETDATE())=0 then '今天' 
 when ord_status='外呼跟进' and ord_whzt='待跟进' and DATEDIFF(day,ord_xcgjsj,GETDATE())=-1 then '明天' 
 when ord_status='外呼跟进' and ord_whzt='待跟进' and DATEDIFF(day,ord_xcgjsj,GETDATE())>=1 then  '过期'
 when ord_status='预约成功' and ord_whzt='已预约' and DATEDIFF(day,ord_ddddsj,GETDATE())=0 then '今天' 
 when ord_status='预约成功' and ord_whzt='已预约' and DATEDIFF(day,ord_ddddsj,GETDATE())=-1 then '明天' 
 when ord_status='预约成功' and ord_whzt='已预约' and DATEDIFF(day,ord_ddddsj,GETDATE())>=1 then  '过期' 
 when ord_status='外呼跟进' and ord_whzt='未联系到' and DATEDIFF(day,ord_xcgjsj,GETDATE())=0 then '今天' 
 when ord_status='外呼跟进' and ord_whzt='未联系到' and DATEDIFF(day,ord_xcgjsj,GETDATE())=-1 then '明天' 
 when ord_status='外呼跟进' and ord_whzt='未联系到' and DATEDIFF(day,ord_xcgjsj,GETDATE())>=1 then  '过期'
 when ord_status='外呼跟进' and ord_sfcj='未成交' and ord_ddzt='未到店' and DATEDIFF(day,ord_ddddsj,GETDATE())=0 then '今天' 
 when ord_status='外呼跟进' and ord_sfcj='未成交' and ord_ddzt='未到店' and DATEDIFF(day,ord_ddddsj,GETDATE())=-1 then '明天' 
 when ord_status='外呼跟进' and ord_sfcj='未成交' and ord_ddzt='未到店' and DATEDIFF(day,ord_ddddsj,GETDATE())>=1 then  '过期'  
end ord_time ,
count(1) ord_count

from order_239
where ord_status in ('外呼跟进','预约成功') 
and DATEDIFF(day,ord_xcgjsj,GETDATE())>= -1
and DATEDIFF(day,ord_ddddsj,GETDATE())>= -1
group by  ord_branch ,
     case when ord_status='外呼跟进' and ord_whzt='待跟进' then '待跟进'
     when ord_status='预约成功' and ord_whzt='已预约' then '已预约'
     when ord_status='外呼跟进' and ord_whzt='未联系到' then '未联系到'
     when ord_status='外呼跟进' and ord_sfcj='未成交' and ord_ddzt='未到店' then '未到店'
     end, 
     case when ord_status='外呼跟进' and ord_whzt='待跟进' and DATEDIFF(day,ord_xcgjsj,GETDATE())=0 then '今天' 
 when ord_status='外呼跟进' and ord_whzt='待跟进' and DATEDIFF(day,ord_xcgjsj,GETDATE())=-1 then '明天' 
     when ord_status='外呼跟进' and ord_whzt='待跟进' and DATEDIFF(day,ord_xcgjsj,GETDATE())>=1 then  '过期'
 when ord_status='预约成功' and ord_whzt='已预约' and DATEDIFF(day,ord_ddddsj,GETDATE())=0 then '今天' 
 when ord_status='预约成功' and ord_whzt='已预约' and DATEDIFF(day,ord_ddddsj,GETDATE())=-1 then '明天' 
 when ord_status='预约成功' and ord_whzt='已预约' and DATEDIFF(day,ord_ddddsj,GETDATE())>=1 then  '过期' 
 when ord_status='外呼跟进' and ord_whzt='未联系到' and DATEDIFF(day,ord_xcgjsj,GETDATE())=0 then '今天' 
 when ord_status='外呼跟进' and ord_whzt='未联系到' and DATEDIFF(day,ord_xcgjsj,GETDATE())=-1 then '明天' 
 when ord_status='外呼跟进' and ord_whzt='未联系到' and DATEDIFF(day,ord_xcgjsj,GETDATE())>=1 then  '过期'
 when ord_status='外呼跟进' and ord_sfcj='未成交' and ord_ddzt='未到店' and DATEDIFF(day,ord_ddddsj,GETDATE())=0 then '今天' 
 when ord_status='外呼跟进' and ord_sfcj='未成交' and ord_ddzt='未到店' and DATEDIFF(day,ord_ddddsj,GETDATE())=-1 then '明天' 
 when ord_status='外呼跟进' and ord_sfcj='未成交' and ord_ddzt='未到店' and DATEDIFF(day,ord_ddddsj,GETDATE())>=1 then  '过期'  
     end

END