获取重复记录中的任一条目

时间:2014-05-19 11:57:29

标签: sql sql-server-2008

这个问题是参考

  

Get specific entry in case of duplicate entry

现在,在这种情况下,不同的值为user Id,其余值相同。

|UserId | First name | Last name | IsRequired | IsDeleted |
     1        harry          tom        true         false
     3        harry          tom        true         false
     3        ram           sham      true         false

我需要两个条目中的任何一个,但上面链接中提到的场景也应该是他们的..

预期结果

| UserId | First name | Last name | IsRequired | IsDeleted |
    1        harry          tom        true         false
    3        ram            sham       true         false

用户ID可以是1 or 3

1 个答案:

答案 0 :(得分:1)

试试这个!

create table users(UserId int, Firstname varchar(30), Lastname varchar(30), IsRequired varchar(5), IsDeleted varchar(5));

insert into users values('1','harry','tom','true','false');
insert into users values('1','harry','tom','false','false');
insert into users values('3','ram','sham','true','false');

select * from
(
select *,rn=ROW_NUMBER()over(partition by UserId order by Firstname desc) from users
)x
where x.rn=1

See demo