连续号码拆分
A表
id startNum endNum
1 3 6
2 13 19
3 22 25
B表
id defeasanceNum
1 5
2 14
3 16
4 24
如上两个表,表A每一行表示一段数字,例如id=1,是3到6,id=2是从13到19,注意表A的id和表B的id,无任何关系,不是外键关系,就是个编号而已,表B中的defeasanceNum,表示A 表中作废的数字,根据表A和表B ,想得到表C
C表
a_id singleNum isDefeasance
1 3 false
1 4 false
1 5 true
1 6 false
2 13 false
2 14 true
2 15 false
2 16 true
2 17 false
2 18 false
2 19 false
3 22 false
3 23 false
3 24 true
3 25 false
作废的号,isDefeasance显示 true
[解决办法]
create table a(id int ,startNum int, endNum int)insert aselect 1 ,3 ,6 union allselect 2 ,13 ,19 union allselect 3 ,22 ,25create table b(id int ,defeasanceNum int)insert bselect 1 ,5 union allselect 2 ,14 union allselect 3 ,16 union allselect 4 ,24select *,(case when exists(select 1 from b where b.defeasanceNum=tb.number) then 'true' else 'false' end) as isDefeasancefrom (select a.id,number from a,master..spt_values where type='p'and number>=a.startNum and number<=a.endNum) tbselect * from a/*id number isDefeasance ----------- ----------- ------------ 1 3 false1 4 false1 5 true1 6 false2 13 false2 14 true2 15 false2 16 true2 17 false2 18 false2 19 false3 22 false3 23 false3 24 true3 25 false*/