22,209
社区成员
发帖
与我相关
我的任务
分享
--> 建立数据表#test1 --数据表#test1里有'unotext'字段记录(一共有1178009行数据,每一行的数据都是基因信息数据)
if object_id('tempdb.dbo.#test1') is not null drop table #test1
go
create table #test1([personid] int,[unotext] nvarchar(20))
insert #test1
select 100001,'29011008032003030' union all
select 100002,'19010027025013018' union all
select 100003,'26028011013010012' union all
select 100004,'09004026021019020' union all
select 100005,'23007028010001032' union all
select 100006,'13004007025006010' union all
select 100007,'04030017015031006' union all
select 100008,'01010027026021003' union all
select 100009,'23019009026001021' union all
select 100010,'09008017023014005' union all
select 100011,'30018009022005020' union all
select 100012,'01017024008002013'
--> 建立数据表#test2 --数据表#test2里有'jytzum'字段记录(一共有1177行数据,每一行的数据都是基因特征)
if object_id('tempdb.dbo.#test2') is not null drop table #test2
go
create table #test2([SSID] nvarchar(20),[jytzum] nvarchar(200))
insert #test2
select 'D004','Cast(SUBSTRING(unotext,2,1)as int)=2*Cast(SUBSTRING(unotext,5,1)as int)' union all
select 'D017','Cast(SUBSTRING(unotext,1,2)as int)%3=0' union all
select 'D028','Cast(SUBSTRING(unotext,11,1)as int)+Cast(SUBSTRING(unotext,17,1)as int)=2*Cast(SUBSTRING(unotext,14,1)as int)' union all
select 'D030','Cast(SUBSTRING(unotext,11,1)as int)=2*Cast(SUBSTRING(unotext,17,1)as int)' union all
select 'D031','Cast(SUBSTRING(unotext,11,1)as int)=2*Cast(SUBSTRING(unotext,2,1)as int)' union all
select 'D032','Cast(SUBSTRING(unotext,11,1)as int)=2*Cast(SUBSTRING(unotext,5,1)as int)' union all
select 'D038','Cast(SUBSTRING(unotext,13,2)as int)-Cast(SUBSTRING(unotext,10,2)as int)=1' union all
select 'D039','Cast(SUBSTRING(unotext,14,1)as int)+Cast(SUBSTRING(unotext,2,1)as int)=2*Cast(SUBSTRING(unotext,17,1)as int)' union all
select 'D042','Cast(SUBSTRING(unotext,14,1)as int)=2*Cast(SUBSTRING(unotext,2,1)as int)' union all
select 'D051','Cast(SUBSTRING(unotext,17,1)as int)=2*Cast(SUBSTRING(unotext,14,1)as int)' union all
select 'D053','Cast(SUBSTRING(unotext,17,1)as int)=2*Cast(SUBSTRING(unotext,5,1)as int)' union all
select 'D056','Cast(SUBSTRING(unotext,2,1)as int)+Cast(SUBSTRING(unotext,8,1)as int)=2*Cast(SUBSTRING(unotext,5,1)as int)' union all
select 'D057','Cast(SUBSTRING(unotext,2,1)as int)=Cast(SUBSTRING(unotext,17,1)as int)' union all
select 'D059','Cast(SUBSTRING(unotext,4,1)as int)=Cast(SUBSTRING(unotext,17,1)as int)' union all
select 'D070','Cast(SUBSTRING(unotext,7,2)as int)%5=0' union all
select 'D074','Cast(SUBSTRING(unotext,8,1)as int)=Cast(SUBSTRING(unotext,13,1)as int)'
--> 建立被测试者名单数据表#test3 --数据表#test3里有'SID'字段记录(每一行的数据都是每一位被测试者基因特征)
if object_id('tempdb.dbo.#test3') is not null drop table #test3
go
create table #test3([persoNM] nvarchar(20),[SID] nvarchar(200))
insert #test3
select 'A被测试者','D028 D039 D051' union all
select 'B被测试者','D042 D059' union all
select 'C被测试者','D032' union all
select 'D被测试者','D004 D017 D030 D031 D038 D053 D056 D057 D070 D074'
if object_id('tempdb.dbo.#test4') is not null drop table #test4
go
create table #test4 ([persoNM] nvarchar(20),[unotext] nvarchar(20))
DECLARE @persoNM NVARCHAR(20),@formula VARCHAR(2000)
DECLARE @SQL VARCHAR(MAX)
DECLARE cur_test CURSOR FOR
select tt3.persoNM,ttt.formula from #test3 as tt3
cross apply (
select stuff((select ' and ('+t2.jytzum +')' from (
select *,convert(xml,'<r><n>'+replace(SID,' ','</n><n>')+'</n></r>') as xssid from #test3
) t3
cross apply (select b.value('.','varchar(100)') as ssid from t3.xssid.nodes('/r/n') as s(b)) tt
left join #test2 as t2 on t2.SSID=tt.ssid
where t3.persoNM=t3.persoNM for xml path('')),1,1,'') as formula
) ttt
open cur_test
FETCH NEXT FROM cur_test INTO @persoNM,@formula
WHILE @@FETCH_STATUS = 0
BEGIN
set @SQL='insert into #test4 select N'''+@persoNM+''' as persoNM, [personid] from #test1 as t1 where 1=1 '+ @formula
print @sql
exec(@SQL)
FETCH NEXT FROM cur_test INTO @persoNM,@formula
end
CLOSE cur_test
DEALLOCATE cur_test
SELECT * FROM #test4
举个生成的动态代码例子,如果觉得不对,你可以修改下:
insert into #test4 select N'A被测试者' as persoNM, [personid] from #test1 as t1 where 1=1 and (Cast(SUBSTRING(unotext,11,1)as int)+Cast(SUBSTRING(unotext,17,1)as int)=2*Cast(SUBSTRING(unotext,14,1)as int)) and (Cast(SUBSTRING(unotext,14,1)as int)+Cast(SUBSTRING(unotext,2,1)as int)=2*Cast(SUBSTRING(unotext,17,1)as int)) and (Cast(SUBSTRING(unotext,17,1)as int)=2*Cast(SUBSTRING(unotext,14,1)as int)) and (Cast(SUBSTRING(unotext,14,1)as int)=2*Cast(SUBSTRING(unotext,2,1)as int)) and (Cast(SUBSTRING(unotext,4,1)as int)=Cast(SUBSTRING(unotext,17,1)as int)) and (Cast(SUBSTRING(unotext,11,1)as int)=2*Cast(SUBSTRING(unotext,5,1)as int)) and (Cast(SUBSTRING(unotext,2,1)as int)=2*Cast(SUBSTRING(unotext,5,1)as int)) and (Cast(SUBSTRING(unotext,1,2)as int)%3=0) and (Cast(SUBSTRING(unotext,11,1)as int)=2*Cast(SUBSTRING(unotext,17,1)as int)) and (Cast(SUBSTRING(unotext,11,1)as int)=2*Cast(SUBSTRING(unotext,2,1)as int)) and (Cast(SUBSTRING(unotext,13,2)as int)-Cast(SUBSTRING(unotext,10,2)as int)=1) and (Cast(SUBSTRING(unotext,17,1)as int)=2*Cast(SUBSTRING(unotext,5,1)as int)) and (Cast(SUBSTRING(unotext,2,1)as int)+Cast(SUBSTRING(unotext,8,1)as int)=2*Cast(SUBSTRING(unotext,5,1)as int)) and (Cast(SUBSTRING(unotext,2,1)as int)=Cast(SUBSTRING(unotext,17,1)as int)) and (Cast(SUBSTRING(unotext,7,2)as int)%5=0) and (Cast(SUBSTRING(unotext,8,1)as int)=Cast(SUBSTRING(unotext,13,1)as int))
insert into #test4 select N'B被测试者' as persoNM, [personid] from #test1 as t1 where 1=1 and (Cast(SUBSTRING(unotext,11,1)as int)+Cast(SUBSTRING(unotext,17,1)as int)=2*Cast(SUBSTRING(unotext,14,1)as int)) and (Cast(SUBSTRING(unotext,14,1)as int)+Cast(SUBSTRING(unotext,2,1)as int)=2*Cast(SUBSTRING(unotext,17,1)as int)) and (Cast(SUBSTRING(unotext,17,1)as int)=2*Cast(SUBSTRING(unotext,14,1)as int)) and (Cast(SUBSTRING(unotext,14,1)as int)=2*Cast(SUBSTRING(unotext,2,1)as int)) and (Cast(SUBSTRING(unotext,4,1)as int)=Cast(SUBSTRING(unotext,17,1)as int)) and (Cast(SUBSTRING(unotext,11,1)as int)=2*Cast(SUBSTRING(unotext,5,1)as int)) and (Cast(SUBSTRING(unotext,2,1)as int)=2*Cast(SUBSTRING(unotext,5,1)as int)) and (Cast(SUBSTRING(unotext,1,2)as int)%3=0) and (Cast(SUBSTRING(unotext,11,1)as int)=2*Cast(SUBSTRING(unotext,17,1)as int)) and (Cast(SUBSTRING(unotext,11,1)as int)=2*Cast(SUBSTRING(unotext,2,1)as int)) and (Cast(SUBSTRING(unotext,13,2)as int)-Cast(SUBSTRING(unotext,10,2)as int)=1) and (Cast(SUBSTRING(unotext,17,1)as int)=2*Cast(SUBSTRING(unotext,5,1)as int)) and (Cast(SUBSTRING(unotext,2,1)as int)+Cast(SUBSTRING(unotext,8,1)as int)=2*Cast(SUBSTRING(unotext,5,1)as int)) and (Cast(SUBSTRING(unotext,2,1)as int)=Cast(SUBSTRING(unotext,17,1)as int)) and (Cast(SUBSTRING(unotext,7,2)as int)%5=0) and (Cast(SUBSTRING(unotext,8,1)as int)=Cast(SUBSTRING(unotext,13,1)as int))
insert into #test4 select N'C被测试者' as persoNM, [personid] from #test1 as t1 where 1=1 and (Cast(SUBSTRING(unotext,11,1)as int)+Cast(SUBSTRING(unotext,17,1)as int)=2*Cast(SUBSTRING(unotext,14,1)as int)) and (Cast(SUBSTRING(unotext,14,1)as int)+Cast(SUBSTRING(unotext,2,1)as int)=2*Cast(SUBSTRING(unotext,17,1)as int)) and (Cast(SUBSTRING(unotext,17,1)as int)=2*Cast(SUBSTRING(unotext,14,1)as int)) and (Cast(SUBSTRING(unotext,14,1)as int)=2*Cast(SUBSTRING(unotext,2,1)as int)) and (Cast(SUBSTRING(unotext,4,1)as int)=Cast(SUBSTRING(unotext,17,1)as int)) and (Cast(SUBSTRING(unotext,11,1)as int)=2*Cast(SUBSTRING(unotext,5,1)as int)) and (Cast(SUBSTRING(unotext,2,1)as int)=2*Cast(SUBSTRING(unotext,5,1)as int)) and (Cast(SUBSTRING(unotext,1,2)as int)%3=0) and (Cast(SUBSTRING(unotext,11,1)as int)=2*Cast(SUBSTRING(unotext,17,1)as int)) and (Cast(SUBSTRING(unotext,11,1)as int)=2*Cast(SUBSTRING(unotext,2,1)as int)) and (Cast(SUBSTRING(unotext,13,2)as int)-Cast(SUBSTRING(unotext,10,2)as int)=1) and (Cast(SUBSTRING(unotext,17,1)as int)=2*Cast(SUBSTRING(unotext,5,1)as int)) and (Cast(SUBSTRING(unotext,2,1)as int)+Cast(SUBSTRING(unotext,8,1)as int)=2*Cast(SUBSTRING(unotext,5,1)as int)) and (Cast(SUBSTRING(unotext,2,1)as int)=Cast(SUBSTRING(unotext,17,1)as int)) and (Cast(SUBSTRING(unotext,7,2)as int)%5=0) and (Cast(SUBSTRING(unotext,8,1)as int)=Cast(SUBSTRING(unotext,13,1)as int))
insert into #test4 select N'D被测试者' as persoNM, [personid] from #test1 as t1 where 1=1 and (Cast(SUBSTRING(unotext,11,1)as int)+Cast(SUBSTRING(unotext,17,1)as int)=2*Cast(SUBSTRING(unotext,14,1)as int)) and (Cast(SUBSTRING(unotext,14,1)as int)+Cast(SUBSTRING(unotext,2,1)as int)=2*Cast(SUBSTRING(unotext,17,1)as int)) and (Cast(SUBSTRING(unotext,17,1)as int)=2*Cast(SUBSTRING(unotext,14,1)as int)) and (Cast(SUBSTRING(unotext,14,1)as int)=2*Cast(SUBSTRING(unotext,2,1)as int)) and (Cast(SUBSTRING(unotext,4,1)as int)=Cast(SUBSTRING(unotext,17,1)as int)) and (Cast(SUBSTRING(unotext,11,1)as int)=2*Cast(SUBSTRING(unotext,5,1)as int)) and (Cast(SUBSTRING(unotext,2,1)as int)=2*Cast(SUBSTRING(unotext,5,1)as int)) and (Cast(SUBSTRING(unotext,1,2)as int)%3=0) and (Cast(SUBSTRING(unotext,11,1)as int)=2*Cast(SUBSTRING(unotext,17,1)as int)) and (Cast(SUBSTRING(unotext,11,1)as int)=2*Cast(SUBSTRING(unotext,2,1)as int)) and (Cast(SUBSTRING(unotext,13,2)as int)-Cast(SUBSTRING(unotext,10,2)as int)=1) and (Cast(SUBSTRING(unotext,17,1)as int)=2*Cast(SUBSTRING(unotext,5,1)as int)) and (Cast(SUBSTRING(unotext,2,1)as int)+Cast(SUBSTRING(unotext,8,1)as int)=2*Cast(SUBSTRING(unotext,5,1)as int)) and (Cast(SUBSTRING(unotext,2,1)as int)=Cast(SUBSTRING(unotext,17,1)as int)) and (Cast(SUBSTRING(unotext,7,2)as int)%5=0) and (Cast(SUBSTRING(unotext,8,1)as int)=Cast(SUBSTRING(unotext,13,1)as int))
if object_id('test3') is not null drop table test3
go
create table test3([persoNM] nvarchar(20),[SID] nvarchar(200))
insert test3
select 'A被测试者','D028 D039 D051' union all
select 'B被测试者','D042 D059' union all
select 'C被测试者','D032' union all
select 'D被测试者','D004 D017 D030 D031 D038 D053 D056 D057 D070 D074'
GO
if object_id('test1') is not null drop table test1
go
create table test1([personid] int,[unotext] nvarchar(20))
insert test1
select 100001,'29011008032003030' union all
select 100002,'19010027025013018' union all
select 100003,'26028011013010012' union all
select 100004,'09004026021019020' union all
select 100005,'23007028010001032' union all
select 100006,'13004007025006010' union all
select 100007,'04030017015031006' union all
select 100008,'01010027026021003' union all
select 100009,'23019009026001021' union all
select 100010,'09008017023014005' union all
select 100011,'30018009022005020' union all
select 100012,'01017024008002013'
GO
if object_id('test2') is not null drop table test2
go
create table test2([SSID] nvarchar(20),[jytzum] nvarchar(200))
insert test2
select 'D004','Cast(SUBSTRING(unotext,2,1)as int)=2*Cast(SUBSTRING(unotext,5,1)as int)' union all
select 'D017','Cast(SUBSTRING(unotext,1,2)as int)%3=0' union all
select 'D028','Cast(SUBSTRING(unotext,11,1)as int)+Cast(SUBSTRING(unotext,17,1)as int)=2*Cast(SUBSTRING(unotext,14,1)as int)' union all
select 'D030','Cast(SUBSTRING(unotext,11,1)as int)=2*Cast(SUBSTRING(unotext,17,1)as int)' union all
select 'D031','Cast(SUBSTRING(unotext,11,1)as int)=2*Cast(SUBSTRING(unotext,2,1)as int)' union all
select 'D032','Cast(SUBSTRING(unotext,11,1)as int)=2*Cast(SUBSTRING(unotext,5,1)as int)' union all
select 'D038','Cast(SUBSTRING(unotext,13,2)as int)-Cast(SUBSTRING(unotext,10,2)as int)=1' union all
select 'D039','Cast(SUBSTRING(unotext,14,1)as int)+Cast(SUBSTRING(unotext,2,1)as int)=2*Cast(SUBSTRING(unotext,17,1)as int)' union all
select 'D042','Cast(SUBSTRING(unotext,14,1)as int)=2*Cast(SUBSTRING(unotext,2,1)as int)' union all
select 'D051','Cast(SUBSTRING(unotext,17,1)as int)=2*Cast(SUBSTRING(unotext,14,1)as int)' union all
select 'D053','Cast(SUBSTRING(unotext,17,1)as int)=2*Cast(SUBSTRING(unotext,5,1)as int)' union all
select 'D056','Cast(SUBSTRING(unotext,2,1)as int)+Cast(SUBSTRING(unotext,8,1)as int)=2*Cast(SUBSTRING(unotext,5,1)as int)' union all
select 'D057','Cast(SUBSTRING(unotext,2,1)as int)=Cast(SUBSTRING(unotext,17,1)as int)' union all
select 'D059','Cast(SUBSTRING(unotext,4,1)as int)=Cast(SUBSTRING(unotext,17,1)as int)' union all
select 'D070','Cast(SUBSTRING(unotext,7,2)as int)%5=0' union all
select 'D074','Cast(SUBSTRING(unotext,8,1)as int)=Cast(SUBSTRING(unotext,13,1)as int)'
GO
--创建结果表
if object_id('result') is not null drop table result
go
CREATE TABLE result([persoNM] nvarchar(20),[personid] int,[unotext] nvarchar(20))
GO
DECLARE @persoNM NVARCHAR(20),@SID NVARCHAR(200)
DECLARE @SQL NVARCHAR(MAX),@WHERE NVARCHAR(MAX)
DECLARE @personid INT,@unotext NVARCHAR(20)
--游标表
DECLARE Test4 CURSOR FOR
SELECT *
FROM test3
OPEN Test4
FETCH NEXT FROM Test4 INTO @persoNM,@SID
WHILE @@FETCH_STATUS = 0
BEGIN
SET @WHERE = ''
SET @personid = NULL
SET @unotext = NULL
SELECT @WHERE = @WHERE + CASE WHEN @WHERE = '' THEN '(' + ISNULL(jytzum,'') + ')' ELSE 'AND (' + ISNULL(jytzum,'') + ')' END
FROM test2
WHERE CHARINDEX(SSID,@SID) > 0
IF @WHERE = ''
SET @WHERE = '1 < 0'
SET @SQL = 'SELECT @P1 = personid,@P2 = unotext FROM test1 where ' + @WHERE
EXEC Sp_executesql @SQL,N'@P1 int output,@P2 NVARCHAR(20) output',@P1 = @personid OUTPUT,@P2 = @unotext OUTPUT
INSERT INTO result
( persoNM, personid, unotext )
VALUES ( @persoNM, -- persoNM - nvarchar(20)
@personid, -- personid - int
@unotext -- unotext - nvarchar(20)
)
FETCH NEXT FROM Test4 INTO @persoNM,@SID
END
CLOSE Test4
DEALLOCATE Test4
SELECT * FROM result
DROP TABLE test1
DROP TABLE test2
DROP TABLE test3
DROP TABLE result
/*
--结果
persoNM personid unotext
-------------------- ----------- --------------------
A被测试者 100005 23007028010001032
B被测试者 100006 13004007025006010
C被测试者 100001 29011008032003030
D被测试者 NULL NULL
*/
SELECT * FROM #test1
WHERE Cast(SUBSTRING(unotext,11,1)as int)=2*Cast(SUBSTRING(unotext,5,1)as int)
/*
--结果
personid unotext
----------- --------------------
100001 29011008032003030
*/
if object_id('tempdb..#test4') is not null drop table #test4
go
create table #test4 ([persoNM] nvarchar(20),[personid] nvarchar(20),[unotext] nvarchar(20))
--'A被测试者','D028 D039 D051'
insert into #test4 select N'A被测试者' as persoNM, [personid],[unotext] from #test1 as t1
WHERE 1=1
and Cast(SUBSTRING(unotext,11,1) as int)+Cast(SUBSTRING(unotext,17,1) as int)=2*Cast(SUBSTRING(unotext,14,1) as int)---D028
and Cast(SUBSTRING(unotext,14,1) as int)+Cast(SUBSTRING(unotext,2,1) as int)=2*Cast(SUBSTRING(unotext,17,1) as int)---D039
and Cast(SUBSTRING(unotext,17,1) as int)=2*Cast(SUBSTRING(unotext,14,1) as int)---D051
--'B被测试者','D042 D059'
insert into #test4 select N'B被测试者' as persoNM, [personid],[unotext] from #test1 as t1
where 1=1
and Cast(SUBSTRING(unotext,14,1) as int)=2*Cast(SUBSTRING(unotext,2,1) as int)---D042
and Cast(SUBSTRING(unotext,4,1) as int)=Cast(SUBSTRING(unotext,17,1) as int)---D059
--'C被测试者','D032'
insert into #test4 select N'C被测试者' as persoNM, [personid],[unotext] from #test1 as t1
where 1=1
and Cast(SUBSTRING(unotext,11,1) as int)=2*Cast(SUBSTRING(unotext,5,1) as int)---D032
--'D被测试者','D003 D013 D017 D026 D028 D038 D040 D048 D060'
insert into #test4 select N'D被测试者' as persoNM, [personid],[unotext] from #test1 as t1
where 1=1
and Cast(SUBSTRING(unotext, 2,1) as int)=2*Cast(SUBSTRING(unotext,17,1) as int)---D003
and Cast(SUBSTRING(unotext, 8,1) as int)=2*Cast(SUBSTRING(unotext,17,1) as int)---D013
and Cast(SUBSTRING(unotext,1,2) as int)%3=0---D017
and Cast(SUBSTRING(unotext,10,2) as int)+Cast(SUBSTRING(unotext,16,2) as int)=2*Cast(SUBSTRING(unotext,13,2) as int)---D026
and Cast(SUBSTRING(unotext,11,1) as int)+Cast(SUBSTRING(unotext,17,1) as int)=2*Cast(SUBSTRING(unotext,14,1) as int)---D028
and Cast(SUBSTRING(unotext,13,2) as int)-Cast(SUBSTRING(unotext,10,2) as int)=1---D038
and Cast(SUBSTRING(unotext,14,1) as int)=2*Cast(SUBSTRING(unotext,11,1) as int)---D040
and Cast(SUBSTRING(unotext,16,2) as int)-Cast(SUBSTRING(unotext,13,2) as int)=1---D048
and Cast(SUBSTRING(unotext,4,2) as int)%3=0---D060
SELECT * FROM #test4 order by persoNM
/*
--结果
persoNM personid unotext
-------------------- ----------- --------------------
A被测试者 100010 23007028010001032
B被测试者 100012 13004007025006010
C被测试者 100002 29011008032003030
C被测试者 100025 31033032006015026
D被测试者 100001 06009016021022023
D被测试者 100003 06009016031032033
D被测试者 100005 06009026031032033
D被测试者 100007 06012016021022023
D被测试者 100009 06012016031032033
D被测试者 100011 06012026031032033
D被测试者 100013 06015016021022023
D被测试者 100015 06015016031032033
D被测试者 100017 06015026031032033
D被测试者 100019 06018026031032033
D被测试者 100021 06021026031032033
D被测试者 100023 06024026031032033
*/
-- 建结果表
create table #result
(persoNM nvarchar(20),
personid int,
unotext nvarchar(20))
-- 计算
declare @persoNM nvarchar(20),@SID nvarchar(200),@tsql nvarchar(max)
declare ap scroll cursor for select persoNM,SID from #test3
open ap
fetch first from ap into @persoNM,@SID
while(@@fetch_status<>-1)
begin
select @tsql=null
select @tsql=isnull(@tsql+' and ','')+c.jytzum
from (select @SID 'SID') a
inner join master.dbo.spt_values b on b.number between 1 and len(a.SID)
and substring(N' '+a.SID,b.number,1)=N' '
inner join #test2 c on substring(a.SID,b.number,charindex(' ',a.SID+' ',b.number)-b.number)=c.SSID
where b.[type]=N'P'
select @tsql=N'insert into #result(persoNM,personid,unotext) '
+N' select N'''+@persoNM+''',[personid],[unotext] '
+N' from #test1 t '
+N' where '+@tsql
exec(@tsql)
fetch next from ap into @persoNM,@SID
end
close ap
deallocate ap
update t
set t.persoNM=N''
from (select *,row_number() over(partition by persoNM order by getdate()) 'rn'
from #result) t
where t.rn>=2
-- 查询结果
select * from #result
/*
persoNM personid unotext
-------------------------------------------------- -------------------------------------------------- --------------------------------------------------
A被测试者 100010 23007028010001032
B被测试者 100012 13004007025006010
C被测试者 100002 29011008032003030
100025 31033032006015026
D被测试者 100001 06009016021022023
100003 06009016031032033
100005 06009026031032033
100007 06012016021022023
100009 06012016031032033
100011 06012026031032033
100013 06015016021022023
100015 06015016031032033
100017 06015026031032033
100019 06018026031032033
100021 06021026031032033
100023 06024026031032033
(16 row(s) affected)
*/
7#结果是
/*
--结果
persoNM personid unotext
-------------------- ----------- --------------------
A被测试者 100010 23007028010001032
B被测试者 100012 13004007025006010
C被测试者 100025 31033032006015026
D被测试者 100023 06024026031032033
*/
也很感谢
楼主盼望的正解是
/*
--需要的结果
persoNM personid unotext
-------------------- ----------- --------------------
A被测试者 100010 23007028010001032
B被测试者 100012 13004007025006010
C被测试者 100002 29011008032003030
100025 31033032006015026
D被测试者 100001 06009016021022023
100003 06009016031032033
100005 06009026031032033
100007 06012016021022023
100009 06012016031032033
100011 06012026031032033
100013 06015016021022023
100015 06015016031032033
100017 06015026031032033
100019 06018026031032033
100021 06021026031032033
100023 06024026031032033
*/
if object_id('tempdb..#test2') is not null drop table #test2
go
create table #test2([SSID] nvarchar(20),[jytzum] nvarchar(200))
insert #test2 -- select * from #test2
select 'D003','Cast(SUBSTRING(unotext,2,1) as int)=2*Cast(SUBSTRING(unotext,17,1) as int)' union all
select 'D013','Cast(SUBSTRING(unotext,8,1) as int)=2*Cast(SUBSTRING(unotext,17,1) as int)' union all
select 'D017','Cast(SUBSTRING(unotext,1,2) as int)%3=0' union all
select 'D026','Cast(SUBSTRING(unotext,10,2) as int)+Cast(SUBSTRING(unotext,16,2) as int)=2*Cast(SUBSTRING(unotext,13,2) as int)' union all
select 'D028','Cast(SUBSTRING(unotext,11,1) as int)+Cast(SUBSTRING(unotext,17,1) as int)=2*Cast(SUBSTRING(unotext,14,1) as int)' union all
select 'D032','Cast(SUBSTRING(unotext,11,1) as int)=2*Cast(SUBSTRING(unotext,5,1) as int)' union all
select 'D038','Cast(SUBSTRING(unotext,13,2) as int)-Cast(SUBSTRING(unotext,10,2) as int)=1' union all
select 'D039','Cast(SUBSTRING(unotext,14,1) as int)+Cast(SUBSTRING(unotext,2,1) as int)=2*Cast(SUBSTRING(unotext,17,1) as int)' union all
select 'D040','Cast(SUBSTRING(unotext,14,1) as int)=2*Cast(SUBSTRING(unotext,11,1) as int)' union all
select 'D042','Cast(SUBSTRING(unotext,14,1) as int)=2*Cast(SUBSTRING(unotext,2,1) as int)' union all
select 'D048','Cast(SUBSTRING(unotext,16,2) as int)-Cast(SUBSTRING(unotext,13,2) as int)=1' union all
select 'D051','Cast(SUBSTRING(unotext,17,1) as int)=2*Cast(SUBSTRING(unotext,14,1) as int)' union all
select 'D059','Cast(SUBSTRING(unotext,4,1) as int)=Cast(SUBSTRING(unotext,17,1) as int)' union all
select 'D060','Cast(SUBSTRING(unotext,4,2) as int)%3=0'
GO
--要的结果是这样的,以A被测试者为例:
A被测试者的基因特征是 D028 D039 D051
SELECT * FROM #test1
WHERE 1=1
and Cast(SUBSTRING(unotext,11,1) as int)+Cast(SUBSTRING(unotext,17,1) as int)=2*Cast(SUBSTRING(unotext,14,1) as int)---D028
and Cast(SUBSTRING(unotext,14,1) as int)+Cast(SUBSTRING(unotext,2,1) as int)=2*Cast(SUBSTRING(unotext,17,1) as int)---D039
and Cast(SUBSTRING(unotext,17,1) as int)=2*Cast(SUBSTRING(unotext,14,1) as int)---D051
/*
--结果
personUM personid unotext
----------- --------------------
A被测试者 100010 23007028010001032
*/
IF @WHERE = ''
SET @WHERE = '1 < 0'
--SET @SQL = 'SELECT @P1 = personid,@P2 = unotext FROM test1 where ' + @WHERE
SET @SQL = 'insert into result ( persoNM, personid, unotext ) select @P1 as persoNM,personid,unotext from test1 where ' + @WHERE
--EXEC Sp_executesql @SQL,N'@P1 int output,@P2 NVARCHAR(20) output',@P1 = @personid OUTPUT,@P2 = @unotext OUTPUT
EXEC Sp_executesql @SQL,N'@P1 NVARCHAR(20)',@P1 = @persoNM
/*
INSERT INTO result
( persoNM, personid, unotext )
VALUES ( @persoNM, -- persoNM - nvarchar(20)
@personid, -- personid - int
@unotext -- unotext - nvarchar(20)
)*/
FETCH NEXT FROM Test4 INTO @persoNM,@SID
if object_id('#test3') is not null drop table #test3
go
create table #test3([persoNM] nvarchar(20),[SID] nvarchar(200))
insert #test3 -- select * from #test3
select 'A被测试者','D028 D039 D051' union all
select 'B被测试者','D042 D059' union all
select 'C被测试者','D032' union all
select 'D被测试者','D003 D013 D017 D026 D028 D038 D040 D048 D060'
GO
if object_id('#test1') is not null drop table #test1
go
create table #test1([personid] int,[unotext] nvarchar(20))
insert #test1 --- select * from #test1
select 100001,'06009016021022023' union all
select 100002,'29011008032003030' union all
select 100003,'06009016031032033' union all
select 100004,'26028011013010012' union all
select 100005,'06009026031032033' union all
select 100006,'19010027025013018' union all
select 100007,'06012016021022023' union all
select 100008,'09004026021019020' union all
select 100009,'06012016031032033' union all
select 100010,'23007028010001032' union all
select 100011,'06012026031032033' union all
select 100012,'13004007025006010' union all
select 100013,'06015016021022023' union all
select 100014,'04030017015031006' union all
select 100015,'06015016031032033' union all
select 100016,'01010027026021003' union all
select 100017,'06015026031032033' union all
select 100018,'23019009026001021' union all
select 100019,'06018026031032033' union all
select 100020,'09008017023014005' union all
select 100021,'06021026031032033' union all
select 100022,'30018009022005020' union all
select 100023,'06024026031032033' union all
select 100024,'12011030004005032' union all
select 100025,'31033032006015026'
GO
if object_id('#test2') is not null drop table #test2
go
create table #test2([SSID] nvarchar(20),[jytzum] nvarchar(200))
insert #test2 -- select * from #test2
select 'D003','Cast(SUBSTRING(unotext,2,1) as int)=2*Cast(SUBSTRING(unotext,17,1) as int)' union all
select 'D013','Cast(SUBSTRING(unotext,8,1) as int)=2*Cast(SUBSTRING(unotext,17,1) as int)' union all
select 'D017','Cast(SUBSTRING(unotext,1,2) as int)%3=0' union all
select 'D026','Cast(SUBSTRING(unotext,10,2) as int)+Cast(SUBSTRING(unotext,16,2) as int)=2*Cast(SUBSTRING(unotext,13,2) as int)' union all
select 'D028','Cast(SUBSTRING(unotext,11,1) as int)+Cast(SUBSTRING(unotext,17,1) as int)=2*Cast(SUBSTRING(unotext,14,1) as int)' union all
select 'D038','Cast(SUBSTRING(unotext,13,2) as int)-Cast(SUBSTRING(unotext,10,2) as int)=1' union all
select 'D040','Cast(SUBSTRING(unotext,14,1) as int)=2*Cast(SUBSTRING(unotext,11,1) as int)' union all
select 'D048','Cast(SUBSTRING(unotext,16,2) as int)-Cast(SUBSTRING(unotext,13,2) as int)=1' union all
select 'D060','Cast(SUBSTRING(unotext,4,2) as int)%3=0'
GO
执行5#大神的代码,借此感谢,同时的确是在学习提高工程中。受益匪浅啊,继续言表
--创建结果表
if object_id('result') is not null drop table result
go
CREATE TABLE result([persoNM] nvarchar(20),[personid] int,[unotext] nvarchar(20))
GO
DECLARE @persoNM NVARCHAR(20),@SID NVARCHAR(200)
DECLARE @SQL NVARCHAR(MAX),@WHERE NVARCHAR(MAX)
DECLARE @personid INT,@unotext NVARCHAR(20)
--游标表
DECLARE Test4 CURSOR FOR
SELECT *
FROM test3
OPEN Test4
FETCH NEXT FROM Test4 INTO @persoNM,@SID
WHILE @@FETCH_STATUS = 0
BEGIN
SET @WHERE = ''
SET @personid = NULL
SET @unotext = NULL
SELECT @WHERE = @WHERE + CASE WHEN @WHERE = '' THEN '(' + ISNULL(jytzum,'') + ')' ELSE 'AND (' + ISNULL(jytzum,'') + ')' END
FROM test2
WHERE CHARINDEX(SSID,@SID) > 0
IF @WHERE = ''
SET @WHERE = '1 < 0'
SET @SQL = 'SELECT @P1 = personid,@P2 = unotext FROM test1 where ' + @WHERE
EXEC Sp_executesql @SQL,N'@P1 int output,@P2 NVARCHAR(20) output',@P1 = @personid OUTPUT,@P2 = @unotext OUTPUT
INSERT INTO result
( persoNM, personid, unotext )
VALUES ( @persoNM, -- persoNM - nvarchar(20)
@personid, -- personid - int
@unotext -- unotext - nvarchar(20)
)
FETCH NEXT FROM Test4 INTO @persoNM,@SID
END
CLOSE Test4
DEALLOCATE Test4
SELECT * FROM result
/*
--结果
persoNM personid unotext
-------------------- ----------- --------------------
A被测试者 100023 06024026031032033
B被测试者 NULL NULL
C被测试者 NULL NULL
D被测试者 100023 06024026031032033
*/
谢谢楼上大神们的帮助,先说神感谢并借用5#的表述,需要实现的步骤是:
1、从#test3中分解出测试者的基因特征
2、根据分解出的基因特征从#test2得到查询条件
3、根据查询条件用动态SQL语句从#test1中得到符合条件的基因信息
楼主盼望的正解是
/*
--需要的结果
persoNM personid unotext
-------------------- ----------- --------------------
A被测试者 100010 23007028010001032
B被测试者 100012 13004007025006010
C被测试者 100002 29011008032003030
100025 31033032006015026
D被测试者 100001 06009016021022023
100003 06009016031032033
100005 06009026031032033
100007 06012016021022023
100009 06012016031032033
100011 06012026031032033
100013 06015016021022023
100015 06015016031032033
100017 06015026031032033
100019 06018026031032033
100021 06021026031032033
100023 06024026031032033
*/
--> 建立数据表#test3 --数据表#test3里有'SID'字段记录(每一行的数据都是每一位被测试者基因特征)
if object_id('tempdb.dbo.#test3') is not null drop table #test3
go
create table #test3([persoNM] nvarchar(20),[SID] nvarchar(200))
insert #test3
select 'A被测试者','D028 D039 D051' union all
select 'B被测试者','D042 D059' union all
select 'C被测试者','D032' union all
select 'D被测试者','D004 D017 D030 D031 D038 D053 D056 D057 D070 D074'
--> 建立数据表#test3 --数据表#test3里有'unotext'字段记录(一共有1178009行数据,每一行的数据都是基因信息数据)
if object_id('tempdb.dbo.#test3') is not null drop table #test3
go
create table #test3([persoNM] int,[SID] nvarchar(20))
insert #test3
select 'A被测试者','D028 D039 D051' union all
select 'B被测试者','D042 D059' union all
select 'C被测试者','D032' union all
select 'D被测试者','D004 D017 D030 D031 D038 D053 D056 D057 D070 D074'
--> 建立数据表#test1 --数据表#test1里有'unotext'字段记录(一共有1178009行数据,每一行的数据都是基因信息数据)
if object_id('tempdb.dbo.#test1') is not null drop table #test1
go
create table #test1([personid] int,[unotext] nvarchar(20))
insert #test1
select 100001,'29011008032003030' union all
select 100002,'19010027025013018' union all
select 100003,'26028011013010012' union all
select 100004,'09004026021019020' union all
select 100005,'23007028010001032' union all
select 100006,'13004007025006010' union all
select 100007,'04030017015031006' union all
select 100008,'01010027026021003' union all
select 100009,'23019009026001021' union all
select 100010,'09008017023014005' union all
select 100011,'30018009022005020' union all
select 100012,'01017024008002013'