日期:2014-05-17  浏览次数:20501 次

如何求倒序数和同尾数的并集?
例如在一个字段[NO]里有记录
 select '01 07 10 23 28 32'
union all select '08 13 17 21 23 32'
union all select '11 17 28 30 31 33'
union all select '08 13 15 26 29 31'
union all select '03 05 23 24 27 32'
union all select '01 12 13 23 30 31'
union all select '06 13 16 21 28 31'
union all select '09 11 16 28 32 33'
union all select '02 03 05 06 18 30'
union all select '05 11 12 13 27 31'
union all select '09 11 12 14 22 33'
union all select '16 17 19 22 31 33'
union all select '03 10 21 22 24 33'
union all select '04 12 18 20 23 32'
union all select '01 07 10 22 32 33'
union all select '05 10 11 23 24 32'
union all select '03 10 14 19 20 30'
union all select '01 13 21 23 25 32'
union all select '07 15 16 22 23 32'
union all select '05 21 23 25 28 32'
union all select '02 08 11 13 24 31'
union all select '10 12 21 22 30 33'

当【NO】字段里出现以下倒序数时
'01','10' 
OR  '02','20'  
OR  '03','30'  
OR  '12','21' 
OR  '23','32'
OR  '13','31'
OR  '11','22','33’
‘OR’以下特征的数据时【尾数有相同的】
'01','11','21','31'  -----尾数均为1
'02','12','22 32'    -----尾数均为2
'03','13','23','33'  -----尾数均为3
'04','14','24'      ----- 尾数均为4
'05','15','25'      ----- 尾数均为5
'06','16','26'      ----- 尾数均为6
'07','17','27'      ----- 尾数均为7
'08','18','28'      ----- 尾数均为8
'09','19','29'      ----- 尾数均为9
'10','20','30'      ----- 尾数均为0

出现以上特征的数据,均需用sql语句完成出现倒序数或尾数有相同的并集查询并显示如下图
并集,倒序数,同尾数

------解决方案--------------------

--上面的复制少了
if object_id('Tempdb..#tb') is not null drop table #tb
 
--建临时表,
--col为原字符串,字段[w0]~[w9]为尾数,值为各个尾数的数量
create table #tb(
col varchar(100) null,
[w0] int null,
[w1] int null,
[w2] int null,
[w3] int null,
[w4] int n