开始的数据declare @a table (a int,b int,c varchar(20))declare @b table (n int)insert @a
select 1,3,'a'
union all
select 3,7,'b'
union all
select 7,20,'c'
union all
select 20,30,'d'
union all
select 30,40,'d'insert @b
select 22select * from @a
a b c
----------- ----------- --------------------
1 3 a
3 7 b
7 20 c
20 30 d
30 40 d
select * from @b
n
-----------
22
我想得到的结果a b c
----------- ----------- --------------------
1 3 a
3 7 b
7 20 c
2 30 d
解释下:
看@b表的数据 22
22在 (20,33)这个区间
那么小于这个的区间 全取出来
然后 把(20,33)这个区间的开始的数据 20-> (22-20 =2)
select 1,3,'a'
union all
select 3,7,'b'
union all
select 7,20,'c'
union all
select 20,30,'d'
union all
select 30,40,'d'insert @b
select 22select * from @a
a b c
----------- ----------- --------------------
1 3 a
3 7 b
7 20 c
20 30 d
30 40 d
select * from @b
n
-----------
22
我想得到的结果a b c
----------- ----------- --------------------
1 3 a
3 7 b
7 20 c
2 30 d
解释下:
看@b表的数据 22
22在 (20,33)这个区间
那么小于这个的区间 全取出来
然后 把(20,33)这个区间的开始的数据 20-> (22-20 =2)
declare @a table (a int,b int,c varchar(20))declare @b table (n int)insert @a
select 1,3,'a'
union all
select 3,7,'b'
union all
select 7,20,'c'
union all
select 20,30,'d'
union all
select 30,40,'d'insert @b
select 22
select a=(case when a<n and b>n then n-a else a end),b,c from
@a a,@b b where a.a<b.na b c
----------- ----------- --------------------
1 3 a
3 7 b
7 20 c
2 30 d(4 行受影响)
else a end,b,c
from @a
where (select * from @b) >a