日期:2014-05-18  浏览次数:20444 次

在重复记录中去掉第一条记录,显示其他重复记录
一个表,表中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 all
select '001','c','f',2 union all
select '002','r','t',3 union all
select '003','44','tt',4 union all
select '003','5','j',5 union all
select '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    2
003  5    j    5
003  5    6    6
*/

------解决方案--------------------
探讨

为什么是order by (select 1)) ,而不是order by ID