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

MSSQL 去掉重复
我现在有张表 里面有三个字段 ID NAME Age

现在只想去掉NAME内有重复的,其它两个都显示出来

select distinct Name,ID,age from 表 ?? //这样好像不行啊

------解决方案--------------------
SQL code

select id,name,age from(
select px=row_number()over(partition by NAME order by(select 1)),* from tbl
)a
where px=1

------解决方案--------------------
SQL code
SELECT * FROM [Users]
WHERE [Id] IN(SELECT MAX([Id]) FROM [Users] GROUP BY [Name])

------解决方案--------------------
SQL code
SELECT * FROM [Users]
WHERE [Id] IN(SELECT MAX([Id]) FROM [Users] GROUP BY [Name])
SELECT * FROM [Users]
WHERE [Id] IN(SELECT Min([Id]) FROM [Users] GROUP BY [Name])

----以上所有版本通用
select id,name,age from(
select px=row_number()over(partition by NAME order by(select 1)),* from tbl
)a
where px=1
---row_number() 只有 2005 版本以上支持

------解决方案--------------------
http://topic.csdn.net/u/20080626/00/43d0d10c-28f1-418d-a05b-663880da278a.html

有FAQ帖子可以参考