泛微待办流程中的执行人可以多选,如果发起人选择的执行人是多个
sql中查询到的数据则是:xx,xx,如:267,298
我的sql server版本是12版本的,还没有sql server 16的string_split函数,所以得自己处理这种数据.
思路:用临时表来处理数据
- 将zxr字段结果以逗号分割拆分为多行,并级联hrmresource表查询出数字所代表的人名,将查询结果插入到 临时表 #temp_daiban_zxrname中
- 将临时表 #temp_daiban_zxrname中拆分开的人名以逗号分割合并为一行,并将结果保存至临时表 #temp_daiban_zxrname_list中
- 最终查询时级联临时表 #temp_daiban_zxrname_list where requestid= #temp_daiban_zxrname_list.requestid来获取人名
首先判断临时表是否已经存在,如果存在则删除
if OBJECT_ID(N'tempdb..#temp_daiban_zxrname',N'U') is not null
DROP TABLE #temp_daiban_zxrname;--项目时表
if OBJECT_ID(N'tempdb..#temp_daiban_zxrname_list',N'U') is not null
DROP TABLE #temp_daiban_zxrname_list;--项目时表
将zxr字段查询结果保存至临时表#temp_daiban_zxrname
zxr字段为text类型,所以需要convert(varchar(200),zxr)转换类型
master..spt_values为数据库常量表
select a.requestid,a.zxr,SUBSTRING(a.zxr,number,CHARINDEX(',',convert(varchar(200),zxr)+',',number)-number) zxrid,c.lastname zxrname
into #temp_daiban_zxrname
from formtable_main_26 a ,master..spt_values b , hrmresource c
where number >= 1 and number < len(convert(varchar(200),zxr))
and type='p'
and SUBSTRING(','+convert(varchar(200),zxr),number,1)=','
and c.id=SUBSTRING(a.zxr,number,CHARINDEX(',',convert(varchar(200),zxr)+',',number)-number)
select * from #temp_daiban_zxrname
将#temp_daiban_zxrname中的zxrname 合并为一行,保存至临时表 #temp_daiban_zxrname_list中
select a.requestid,
stuff((select ','+b.zxrname from #temp_daiban_zxrname b where a.requestid=b.requestid for xml path('')),1,1,'') zxrlist
into #temp_daiban_zxrname_list
from #temp_daiban_zxrname a
select * from #temp_daiban_zxrname_list
最终查询
select
a.requestId '请求id',
a.djxh '单据序号', --单据序号
a.rq '发起日期', -- 发起日期
a.sqr '发起人id', -- 发起人id
b.lastname '发起人姓名', --发起人姓名
a.zxr '执行人id', --执行人id
i.zxrlist '执行人', -- 执行人姓名
a.ms '描述', -- 描述
a.jzrq '截止日期', -- 截止日期
a.sjwcrq '实际完成日期', -- 实际完成日期
a.xlpf '评分', -- 评分
iif(a.sjwcrq!='' and a.sjwcrq is not null,concat(convert(decimal(18,1),DATEDIFF(mi, a.rq, iif(a.sjwcrq='',null,sjwcrq))/60.0),'h'),null) '交付时长', --交付时长
case
when DATEDIFF(s, iif(a.jzrq='',null,a.jzrq), GETDATE())>0 then '超期'
else iif(DATEDIFF(s, iif(a.jzrq='',null,a.jzrq), iif(a.sjwcrq='',null,sjwcrq))>0 , '超期','未超期')
END '超期情况', --超期情况
h.nodename '当前节点',
g.lastname '当前未操作者', ----------当前未操作者
e.isreject,
concat(iif(e.iscomplete !=1,convert(decimal(18,1),DATEDIFF(mi, concat(e.receivedate,' ',e.receivetime), GETDATE())/60.0),0),'h') '当前节点停留时间',-----------当前节点停留时间
case
when DATEDIFF(s, iif(a.jzrq='',null,a.jzrq), GETDATE())>0 then '超期'
when DATEDIFF(s, iif(a.jzrq='',null,a.jzrq), iif(a.sjwcrq='',null,sjwcrq))>0 then '超期'
when (select (select nodename from workflow_nodebase where workflow_nodebase.id=workflow_requestbase.currentnodeid) from workflow_requestbase where requestid=a.requestid) ='归档' then '归档'
else '进行'
END '流程状态(进行,超期,归档)', -------流程状态(进行,超期,归档)
e.iscomplete '是否完成'
from formtable_main_26 a
inner join HrmResource b on a.sqr=b.id
inner join workflow_requestbase d on a.requestid=d.requestid
inner join workflow_currentoperator e on a.requestid=e.requestid and e.nodeid=d.currentnodeid and e.preisremark <> 1 and e.isreject is null
inner join hrmresource g on e.userid=g.id
inner join workflow_nodebase h on h.id=d.currentnodeid
inner join #temp_daiban_zxrname_list i on i.requestid=a.requestid
order by e.iscomplete desc