A-A+

利用SQL对数据库实行数据拆分与组合

2016年08月03日 Linux 数据库 暂无评论 阅读 785 次

利用SQL对数据库实行数据拆分与组合实现提供以下几种方案:

方法一:

WITH CTE AS (
SELECT A.Id,A.[Uid],UserName FROM (
SELECT A.[id], REPLACE(Split.a.value('.', 'NVARCHAR(max)'),'''','') AS [Uid]
FROM
(
SELECT [id], CAST ('<M>' + REPLACE(Uid, ',', '</M><M>') + '</M>' AS XML) AS [Uid] FROM A
) AS A CROSS APPLY [Uid].nodes ('/M') AS Split(a)

) A INNER JOIN dbo.B B ON A.[Uid]=B.[Uid]
)
SELECT DISTINCT classList=stuff((select ',' +UserName from CTE B WHERE A.Id=B.Id for xml path('')),1,1,'') FROM CTE A

 方法二:

SELECT t.id,classList=STUFF((
SELECT ','+tt1.UserName
FROM A tt left join B tt1 ON ''+tt.Uid+',' like '%'+tt1.Uid+',%'
WHERE tt.id=t.id FOR XML PATH('')),1,1,'')
FROM A t
ORDER BY t.id

方法三:

select id, (select UserName from B where Uid=t02.item) c into #ttt from dbo.A as t01 cross apply dbo.split(t01.Uid,',') as t02

select id,stuff((select ','+c from dbo.#ttt as tb01 where tb01.id=tb02.id for xml path('')),1,1,'') as classList

from dbo.#ttt as tb02 group by id

drop table #ttt

方法三需要添加内置方法:

create function split(
@string varchar(255),
@separator varchar(255)
)returns @array table(item varchar(255))
as
begin
declare @begin int,@end int,@item varchar(255)
set @begin = 1
set @end=charindex(@separator,@string,@begin)
while(@end<>0)
begin
set @item = substring(@string,@begin,@[email protected])
insert into @array(item) values(@item)
set @begin = @end+1
set @end=charindex(@separator,@string,@begin)
end
set @item = substring(@string,@begin,len(@string)[email protected])
if (len(@item)>0)
insert into @array(item) values(substring(@string,@begin,len(@string)[email protected]))
return
end

下面们直接看结果吧:

表A:

表B:

最终结果:

跟根据讨论给出的方法,我个人给出这样的三种,如果有不对之处,请多多指教。

标签:

给我留言

Copyright © SEARU.ORG 保留所有权利.   Theme  Ality 网站地图 360网站安全检测平台

用户登录

分享到: