您的位置:首页 > 数据库

SQLserver 排除重复项

2012-04-12 11:09 120 查看
方法1:使用distinct

方法2:使用ROW_NUMBER()函数

如:

select * from (select ROW_NUMBER() over(Partition by name order by ID asc) as rowid,t.*

from t) t1

where t1.rowid=1

解释:

Partition by(根据姓名排重) name order by ID(根据ID排序) asc

此段SQL的意思为。

只取ROWID为1的数据。因为ROWID通过ROW_NUMBER()函数获取的。如果NAME 相同的有几位,

where t1.rowid=1为限定条件,只取第一位。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: