sql server查询结果中是以逗号分割的数据处理方式

泛微待办流程中的执行人可以多选,如果发起人选择的执行人是多个

sql中查询到的数据则是:xx,xx,如:267,298

我的sql server版本是12版本的,还没有sql server 16的string_split函数,所以得自己处理这种数据.

思路:用临时表来处理数据

  1. 将zxr字段结果以逗号分割拆分为多行,并级联hrmresource表查询出数字所代表的人名,将查询结果插入到 临时表 #temp_daiban_zxrname中
  2. 将临时表 #temp_daiban_zxrname中拆分开的人名以逗号分割合并为一行,并将结果保存至临时表 #temp_daiban_zxrname_list中
  3. 最终查询时级联临时表 #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

  • 7
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值