SQLserver 排除重复项

方法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为限定条件,只取第一位。

原文地址:https://www.cnblogs.com/Jack_G/p/2443596.html