当前位置: 代码迷 >> Sql Server >> MSSQL 去掉重复,该怎么处理
  详细解决方案

MSSQL 去掉重复,该怎么处理

热度:23   发布时间:2016-04-27 12:20:32.0
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)awhere 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)awhere px=1---row_number() 只有 2005 版本以上支持
------解决方案--------------------
http://topic.csdn.net/u/20080626/00/43d0d10c-28f1-418d-a05b-663880da278a.html

有FAQ帖子可以参考
  相关解决方案