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

如何删除字段里重复的数据(但保留该重复数值),并且按照从小到大的顺序排列在一个字段,还要统计重复数值的个数
如何删除字段里重复的数据(但保留该重复数值),并且按照从小到大的顺序排列在一个字段,还要统计重复数值的个数

例如在表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&n