当前位置: 代码迷 >> Sql Server >> 在重复记录中去掉第一条记录,显示其他重复记录解决办法
  详细解决方案

在重复记录中去掉第一条记录,显示其他重复记录解决办法

热度:69   发布时间:2016-04-27 14:32:10.0
在重复记录中去掉第一条记录,显示其他重复记录
一个表,表中fie1字段有重复记录,想查询fie1字段有重复的记录,并且显示除去重复的第一条的其他重复记录。
例如:
fie1 fie2 fie3 ID
001 a v 1
001 c f 2 
002 r t 3
003 44 tt 4
003 5 j 5
003 5 6 6

查询结果为:
fie1 fie2 fie3 ID
001 c f 2
003 5 j 5
003 5 6 6



------解决方案--------------------
SQL SERVER 2000?2005?2008?
------解决方案--------------------
SQL code
--> 测试数据: [tablefile]if object_id('[tablefile]') is not null drop table [tablefile]create table [tablefile] (fie1 varchar(3),fie2 varchar(2),fie3 varchar(2),ID int)insert into [tablefile]select '001','a','v',1 union allselect '001','c','f',2 union allselect '002','r','t',3 union allselect '003','44','tt',4 union allselect '003','5','j',5 union allselect '003','5','6',6;with maco as(select row_number() over (partition by fie1 order by (select 1)) as rid,* from [tablefile])select fie1,fie2,fie3,ID from maco t where rid>1/*fie1 fie2 fie3 ID---- ---- ---- -----------001  c    f    2003  5    j    5003  5    6    6*/
------解决方案--------------------
探讨

为什么是order by (select 1)) ,而不是order by ID
  相关解决方案