IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[report_20_data]') AND type = 'P')
DROP PROCEDURE [dbo].[report_20_data]
GO
CREATE PROCEDURE report_20_data
@cmpid int,@branchid int,@userdpid int,@userotid int,@useruid int,@tableid varchar(8000),@objids varchar(8000),@pagesize int,@md_jdrq varchar(512)
,@md_wcrq varchar(512)
,@md_gs varchar(512)
,@md_bm varchar(512)
,@md_xqlx 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_whry varchar(10),
    ord_branch varchar(100),
    ord_tocore decimal(18,2) default 0.0,--总分
    ord_createdepart varchar(100),--部门
    ord_wcsl int default 0,   --完成数量
    ord_pjcl decimal(18,2) default 0.0,     --平均处理时间 
    ord_pjxy decimal(18,2) default 0.0,     --平均响应时间
    ord_pjdf decimal(18,2) default 0.0    --平均得分
)

insert into #temp(tableid, ord_branch,ord_createdepart, ord_whry, ord_wcsl, ord_pjcl, ord_pjxy)
select DISTINCT 'tb_report1' tableid, ord_branch,ord_createdepart, ord_whry, count(ord_id), avg(ord_clhs1), avg(ord_wxysj2)
from order_243
where ord_clzt = '已解决'
and  ( ord_whwcsj >= @md_jdrq + ' 00:00:00' and ord_whwcsj <= @md_wcrq + ' 23:59:59')
and ord_branch=@md_gs
and ord_createdepart = @md_bm
and (@md_xqlx = '' or ord_xqlx = @md_xqlx)
group by ord_branch,ord_createdepart,ord_whry

update #temp set ord_tocore =(
select sum(ord_dfqk) from order_243 a
    where #temp.ord_branch=a.ord_branch
    and #temp.ord_createdepart=a.ord_createdepart
    and a.ord_clzt='已解决'
    and (@md_xqlx = '' or ord_xqlx = @md_xqlx)
    and  ( ord_whwcsj >= @md_jdrq + ' 00:00:00' and ord_whwcsj <= @md_wcrq + ' 23:59:59')
    and #temp.ord_whry=a.ord_whry
    )
    
update #temp set ord_pjdf = ord_tocore/ord_wcsl

select * from  #temp
END