首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

大伙儿帮帮忙,看着能不能实现感觉比较难的sql语句

2012-08-13 
大家帮帮忙,看着能不能实现感觉比较难的sql语句维护记录港389 油压上限值 16.00修改为: 3.00 | 李雷港65

大家帮帮忙,看着能不能实现感觉比较难的sql语句


维护记录
港389 油压上限值 16.00修改为: 3.00 | 李雷  
港65 油压上限值 16.00修改为: 3.00
港356 油压上限值 16.00修改为: 3.00 | 李雷 红香
港356 油压上限值 16.00修改为: 3.00 | 范红 红香
港356 油压下限值 1.00修改为: 0.00 | 李雷 李伟

现在想实现的结果是

维护记录 维护人 审核人
港356 油压上限值 16.00修改为: 3.00 李雷 红香
港356 油压上限值 16.00修改为: 3.00 范红 红香
港356 油压下限值 1.00修改为: 0.00 李雷 李伟

维护记录是一个字段想实现变成三个字段 ,有的记录里面有竖杠杠有 的没有竖杠杠,只要有竖杠杠的后面就有维护人,单不一定有审核人,维护人和审核人之间有好几个空格分开
请问大家这个怎样给它实现查询完后出来三个字段

[解决办法]
前两条记录 根据什么条件 被排除了?
[解决办法]

SQL code
create FUNCTION f_splitSTR(@s   varchar(8000),   --待分拆的字符串@split varchar(10)     --数据分隔符)RETURNS @re TABLE(row tinyint,col varchar(100))ASBEGIN    DECLARE @splitlen int,            @Row tinyint    set @row = 1    SET @splitlen=LEN(@split+'a')-2    WHILE CHARINDEX(@split,@s)>0    BEGIN        if (len(replace( LEFT(@s,CHARINDEX(@split,@s)-1),' ',''))>=1)        begin                        INSERT @re VALUES(@row,LEFT(@s,CHARINDEX(@split,@s)-1))            set @Row = @row + 1        end        SET @s=STUFF(@s,1,CHARINDEX(@split,@s)+@splitlen,'')    END    if (len(replace(@s,' ',''))>=1)    begin        INSERT @re VALUES(@row,@s)    end    RETURNENDGOdeclare @t table (r    nvarchar(64))insert into @t select '港389 油压上限值 16.00修改为: 3.00 | 李雷' union allselect '港65 油压上限值 16.00修改为: 3.00' union allselect '港356 油压上限值 16.00修改为: 3.00 | 李雷 红香' union allselect '港356 油压上限值 16.00修改为: 3.00 | 范红 红香' union allselect '港356 油压下限值 1.00修改为: 0.00 | 李雷 李伟' select case when charindex('|',r)>= 1 then left(r, charindex('|',r)-1)       else r end as [维护记录],       (select col from dbo.f_splitSTR(case when charindex('|',r)>= 1 then right(r, len(r)-charindex('|',r))       else null end ,' ') where row = 1 ) as [维护人],        (select col from dbo.f_splitSTR(case when charindex('|',r)>= 1 then right(r, len(r)-charindex('|',r))       else null end ,' ') where row = 2 ) as [审核人]from @t a /*(5 行受影响)维护记录                                                             维护人                                                                                                  审核人---------------------------- ---------------------------------------------------------------- ----------------------------------------------------------------港389 油压上限值 16.00修改为: 3.00                                        李雷                                                                                                   NULL港65 油压上限值 16.00修改为: 3.00                                         NULL                                                                                                 NULL港356 油压上限值 16.00修改为: 3.00                                        李雷                                                                                                   红香港356 油压上限值 16.00修改为: 3.00                                        范红                                                                                                   红香港356 油压下限值 1.00修改为: 0.00                                         李雷                                                                                                   李伟(5 行受影响)*/
[解决办法]
--准备测试数据
create table a_R(WR nvarchar(100))
insert into a_R
values('港389 油压上限值 16.00修改为: 3.00 | 李雷 ')
insert into a_R
values('港65 油压上限值 16.00修改为: 3.00')
insert into a_R
values('港356 油压上限值 16.00修改为: 3.00 | 李雷 红香')
insert into a_R
values('港356 油压上限值 16.00修改为: 3.00 | 范红 红香')
insert into a_R
values('港356 油压下限值 1.00修改为: 0.00 | 李雷 李伟')

--实现你要的查询结果
select WR, (case when WR like '% | %' then Left(WR,Charindex(' | ',WR,1)-1) else WR end) as [维护记录], 


(case when WR like '% | %' then (case when Substring(WR, Charindex(' | ',WR,1)+3, Len(WR)-Charindex(' | ',WR,1)-2) like '% %' then Substring(WR,Charindex(' | ',WR,1)+3,Charindex(' ',Substring(WR,Charindex(' | ',WR,1)+3,Len(WR)-Charindex(' | ',WR,1)-2),1)-1) else Substring(WR, Charindex(' | ',WR,1)+3, Len(WR)-Charindex(' | ',WR,1)-2) end) else '' end) as [维护人],
(case when WR like '% | %' then (case when Substring(WR, Charindex(' | ',WR,1)+3, Len(WR)-Charindex(' | ',WR,1)-2) like '% %' then Substring(WR,Charindex(' | ',WR,1)+2+Charindex(' ',Substring(WR,Charindex(' | ',WR,1)+3,Len(WR)-Charindex(' | ',WR,1)-2),1),LEN(WR)-Charindex(' | ',WR,1)-1-Charindex(' ',Substring(WR,Charindex(' | ',WR,1)+3,Len(WR)-Charindex(' | ',WR,1)-2),1)) else '' end) else '' end) as [审核人]
from a_R

[解决办法]
SQL2000,一条select语句实现.

SQL code
create table xi(维护记录 varchar(500))insert into xiselect '港389 油压上限值 16.00修改为: 3.00 | 李雷' union allselect '港65 油压上限值 16.00修改为: 3.00' union allselect '港356 油压上限值 16.00修改为: 3.00 | 李雷 红香' union allselect '港356 油压上限值 16.00修改为: 3.00 | 范红 红香' union allselect '港356 油压下限值 1.00修改为: 0.00 | 李雷 李伟'select rtrim(substring(维护记录,1,case charindex(' | ',维护记录) when 0 then 200 else charindex(' | ',维护记录) end)) '维护记录',       substring(isnull(substring(维护记录,len(rtrim(substring(维护记录,1,case charindex(' | ',维护记录) when 0 then 200 else charindex(' | ',维护记录) end)))+1,200),''),3,charindex(' ',isnull(substring(维护记录,len(rtrim(substring(维护记录,1,case charindex(' | ',维护记录) when 0 then 200 else charindex(' | ',维护记录) end)))+1,200),''),3)) '维护人',       substring(isnull(substring(维护记录,len(rtrim(substring(维护记录,1,case charindex(' | ',维护记录) when 0 then 200 else charindex(' | ',维护记录) end)))+1,200),''),case charindex(' ',isnull(substring(维护记录,len(rtrim(substring(维护记录,1,case charindex(' | ',维护记录) when 0 then 200 else charindex(' | ',维护记录) end)))+1,200),''),4) when 0 then 200 else charindex(' ',isnull(substring(维护记录,len(rtrim(substring(维护记录,1,case charindex(' | ',维护记录) when 0 then 200 else charindex(' | ',维护记录) end)))+1,200),''),4) end,200) '审核人'from xi/*维护记录                                                   维护人     审核人-------------------------------------------------- -------- --------港389 油压上限值 16.00修改为: 3.00                      李雷      港65 油压上限值 16.00修改为: 3.00                                    港356 油压上限值 16.00修改为: 3.00                      李雷       红香港356 油压上限值 16.00修改为: 3.00                      范红       红香港356 油压下限值 1.00修改为: 0.00                       李雷       李伟(5 row(s) affected)*/ 

热点排行
Bad Request.