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

把尾数一样的统计出来!
有一个数据表tb22,以下数据在一个字段里,字段名'NOTEXT'

'03 05 09 15 20 25'
'01 05 13 15 21 25'
'03 05 15 22 24 25'
'05 08 11 13 15 25'
'07 08 15 23 25 32'
'02 13 19 20 26 27'
'04 11 18 19 26 31'
'07 10 17 23 24 32'
.......
......一共有37000条记录

其中有2个尾数相同的、或者是3个尾数、或者4个尾数相同的、或者有两位同号的。
请问
如何把有2个或3个或4个的相同尾数或两位同号的每条数据统计出来?

举例
'22','33'    -----首尾数均为1个数,把'22','33'同时有的统计出来
'11','33'    -----首尾数均为1个数,把'11','33'同时有的统计出来
'11','22'    -----首尾数均为1个数,把'11','22'同时有的统计出来
'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

大侠指点言谢!
相同的尾数统计

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

create table #tb(col varchar(100))
insert into #tb
select '03 05 09 15 20 25'
union all select '01 05 13 15 21 25'
union all select '03 05 15 22 24 25'
union all select '05 08 11 13 15 25'
union all select '07 08 15 23 25 32'
union all select '02 13 19 20 26 27'
union all select '04 11 18 19 26 31'
union all select '07 10 17 23 24 32'
go


;with cte as 
(
select id=ROW_NUMBER() over(Order by getdate()),col from #tb
),
cte2 as 
(
select id,right(ss,1) as No,count(*) as count
from 
(select id,substring(col,number,charindex(' ',col+' ',number)-number) as ss 
from cte,master..spt_values  
where type='P' and number>=1 and number<=len(col) and substring(' '+col,number,1)=' '
)t
group by id,right(ss,1)
having count(*)>=2

union all
select id,a1,count(*)+1 as num
from 
(
select id,case when right(ss,1)=left(ss,1) and left(ss,1)<>'0' then left(ss,1) end as a1
from 
(
select id,substring(col,number,charindex(' ',col+' ',number)-number) as ss 
from cte,master..spt_values  
where type='P' and number>=1 and number<=len(col) and substring(' '+col,number,1)=' '
)t
)a
where isnull(a1,'')<>''
group by id,a1
)


select&nbs