34,838
社区成员




--看懂了.
create table ta(col1 varchar(10),start int,[end] int,col4 varchar(20))
insert into ta select
'exon1', 17 , 57 ,'12001.m06748' union select
'exon2', 156 , 418 ,'12001.m06748' union select
'exon3', 1159 , 1257 ,'12001.m06748' union select
'exon1', 9450 , 10615 ,'12001.m06750' union select
'exon2', 10708 , 11073 ,'12001.m06750' union select
'exon3', 11161 , 11239 ,'12001.m06750' union select
'exon4', 11771 , 11973 ,'12001.m06750' union select
'exon1', 19643 , 20083 ,'12001.m06752' union select
'exon2', 20374 , 20649 ,'12001.m06752' union select
'exon3', 20764 , 20835 ,'12001.m06752' union select
'exon4', 21294 , 21379 ,'12001.m06752'
go
select col1 = 'ray' + cast(m.px as varchar) ,
start = m.[end] + 1 ,
[end] = n.start - 1 ,
m.col4 from
(select * , px = (select count(1) from ta where col4 = t.col4 and start < t.start ) + 1 from ta t) m,
(select * , px = (select count(1) from ta where col4 = t.col4 and start < t.start ) + 1 from ta t) n
where m.col4 = n.col4 and m.px = n.px - 1
order by m.col4 , col1
drop table ta
/*
col1 start end col4
--------------------------------- ----------- ----------- --------------------
ray1 58 155 12001.m06748
ray2 419 1158 12001.m06748
ray1 10616 10707 12001.m06750
ray2 11074 11160 12001.m06750
ray3 11240 11770 12001.m06750
ray1 20084 20373 12001.m06752
ray2 20650 20763 12001.m06752
ray3 20836 21293 12001.m06752
(所影响的行数为 8 行)
*/
create table ta(col1 varchar(10),start int,[end] int,col4 varchar(20))
insert into ta select
'exon1', 17 , 57 ,'12001.m06748' union select
'exon2', 156 , 418 ,'12001.m06748' union select
'exon3', 1159 , 1257 ,'12001.m06748' union select
'exon1', 9450 , 10615 ,'12001.m06750' union select
'exon2', 10708 , 11073 ,'12001.m06750' union select
'exon3', 11161 , 11239 ,'12001.m06750' union select
'exon4', 11771 , 11973 ,'12001.m06750' union select
'exon1', 19643 , 20083 ,'12001.m06752' union select
'exon2', 20374 , 20649 ,'12001.m06752' union select
'exon3', 20764 , 20835 ,'12001.m06752' union select
'exon4', 21294 , 21379 ,'12001.m06752'
go
select *
from (
select stuff(col1,1,4,'ray') as col1,
start=(select [end] +1 from ta where a.col1 = col1 and col4 = a.col4),
[end]=(select [start] - 1 from ta where cast(stuff(a.col1,1,4,'') as int) = cast(stuff(col1,1,4,'') as int)-1 and col4 = a.col4),
col4
from ta a) b
where [end] is not null
order by col4,col1
drop table ta
/*
col1 start end col4
--------- ----------- ----------- --------------------
ray1 58 155 12001.m06748
ray2 419 1158 12001.m06748
ray1 10616 10707 12001.m06750
ray2 11074 11160 12001.m06750
ray3 11240 11770 12001.m06750
ray1 20084 20373 12001.m06752
ray2 20650 20763 12001.m06752
ray3 20836 21293 12001.m06752
(所影响的行数为 8 行)
*/