如何删除字段里重复的数据(但保留该重复数值),并且按照从小到大的顺序排列在一个字段,还要统计重复数值的个数
例如在表tb1里有17778行数据,数据在字段 [notext] 里,TTD字段代表数据的ID,请问如何用sql语句完成以下要求:
select '04 13 11 14 09 06 08 13 02 01 13 13 10 09 15'
union all select '13 07 04 00 12 10 12 03 05 05 05 05 08 07 16'
union all select '09 04 13 14 11 10 12 01 04 06 13 13 04 13 03'
union all select '11 07 07 12 10 10 12 02 12 02 11 11 07 06 07'
union all select '10 07 02 14 13 14 00 00 01 02 04 04 03 12 03'
union all select '06 04 10 13 13 15 01 04 01 02 16 16 09 08 10'
union all select '05 04 11 08 09 12 14 11 03 08 10 10 08 07 11'
union all select '08 08 05 12 14 02 04 00 06 03 01 01 05 04 12'
union all select '07 08 01 13 00 05 07 15 08 04 11 11 02 11 13'
union all select '02 04 05 01 05 11 13 01 07 01 08 02 06 05 16'
现在需要完成
1、请按照TDD排序(如下图顺序很重要);
2、出现数值 00 就删除掉;
3、删除重复的数据(但保留该重复数值),并且按照从小到大的顺序排列在一个字段 [MMD]里;
4、把重复的数据统计出来,按照重复的次数分别写在字段[RT2],[RT3],[RT4],[RT5],[RT6],[RT7]里,查询结果如下图所示
----[RT2]代表重复2次的数据,[RT3]代表重复3次的数据,以此类推。
可参考http://bbs.csdn.net/topics/390625528?page=1#post-395896477
重复数据删除,数据合并,统计
------解决方案--------------------
create table #tb(id int identity(1,1),notext varchar(100))
insert into #tb(notext)
select'13 07 04 00 12 10 12 03 02 01 13 13 10 09 15'
union all select'09 04 13 14 11 10 12 01 05 05 05 05 08 07 16'
union all select'11 07 07 12 10 10 12 02 04 06 13 13 04 13 03'
union all select'10 07 02 14 13 14 00 00 12 02 11 11 07 06 07'
union all select'06 04 10 13 13 15 01 04 01 02 04 04 03 12 03'
union all select'05 04 11 08 09 12 14 11 01 02 16 16 09 08 10'
union all select'08 08 05 12 14 02 04 00 03 08 10 10 08 07 11'
union all select'07 08 01 13 00 05 07 15 06 03 01 01 05 04 12'
union all select'02 04 05 01 05 11 13 01 08 04 11 11 02 11 13'
union all select'04 07 03 11 00 07 09 01 07 01 08 02 06 05 16'
union all select'01 05 05 11 01 09 11 07 02 01 12 06 04 13 02'
union all select'06 11 01 10 01 10 12 01 03 10 02 12 02 11 16'
union all select'00 06 04 08 00 10 12 08 09 05 04 14 04 13 16'
union all select'00 07 05 04 13 08 10 01 10 02 11 05 03 12 06'
union all select'06 14 06 04 14 10 12 10 06 07 08 02 04 13 16'
union all select'15 08 08 08 03 00 02 00 02 06 02 12 05 14 10'
go
create function dbo.fn_text (@notext varchar(100))
returns varchar(100)
as
begin
declare @t table(col char(2))
declare @s varchar(100),@v varchar(2)
set @s=@notext
while len(@s)>0
begin
set @v=left(@s,2)
if @v<>'00'
insert into @t(col) values(left(@s,2))
set @s=stuff(@s,1,3,'')
end
set @s=''
select @s=@s+' '+col from (select distinct col from @t)t order by col
set @s=stuff(@s,1,1,'')
return (@s)
end
go
create function dbo.fn_text2 (@notext varchar(100))