--利用分组函数进行查询
1.select * from(select ROW_NUMBER() over(partition by 序号 order by 姓名) as a,* from table) as b
where a>1
--利用group by进行查询
2.SELECT 序号, 姓名, count(*)
FROM table
GROUP BY 序号, 姓名
HAVING count(*) > 1
- -话说你应该是应聘的面试题做不下去了吧,最近几个月这个问题都被问三四次了
--姓名重复的数据:
select a.*
from tableName a
where exists(select 1 from tablename b where b.姓名=a.姓名 and b.序号<>a.序号)
是否性能,需要结合索引来完成,如果序号作为主键的话,那么可以考虑在‘姓名’创建索引,
create nonclustered index ix_tablename_x1 on TableName(姓名)
改写查询语句:
select a.*
from tableName a with(index(ix_tablename_x1))
where exists(select 1 from tablename b where b.姓名=a.姓名 and b.序号<>a.序号)
--联系代码1重复的数据:
select a.*
from tableName a
where exists(select 1 from tablename b where b.联系代码1=a.联系代码1 and b.序号<>a.序号)
是否性能,需要结合索引来完成,如果序号作为主键的话,那么可以考虑在‘联系代码1’创建索引,
create nonclustered index ix_tablename_x1 on TableName(联系代码1)
改写查询语句:
select a.*
from tableName a with(index(ix_tablename_x1))
where exists(select 1 from tablename b where b.联系代码1=a.联系代码1 and b.序号<>a.序号)