求一个在同一张表中查询出重复记录ID的方法

gdczch 2013-01-23 05:03:10
求一个在同一张表中查询出重复记录ID的语句,比如
表A中有重复数据
序号:1 姓名:AA 联系代码1:00001 联系代码2:null
序号:3 姓名:AA 联系代码1:00005 联系代码2:00001
序号:5 姓名:AA 联系代码1:00001 联系代码2:00003

求语句如何能查询出重复记录的序号3、5呢?
由于数据表中有几百万条数据,所以写法需要优点。感谢!
...全文
542 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
以学习为目的 2013-01-28
  • 打赏
  • 举报
回复
4楼,8楼的楼主可以试试~
风暴偷鸡队 2013-01-27
  • 打赏
  • 举报
回复
--利用分组函数进行查询 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 - -话说你应该是应聘的面试题做不下去了吧,最近几个月这个问题都被问三四次了
szm341 2013-01-27
  • 打赏
  • 举报
回复
引用 5 楼 xuanyuan0205 的回复:
引用 2 楼 szm341 的回复:select 序号,姓名 from tb t where exists( select 1 from tb where t.姓名=姓名 group by 姓名 having count(*)>1 and min(序号)<t.序号 ) and min(序号)<t.序号 应该是 and max(序号)<t.序号吧?
lz要找出3、5,也就是重复值中比最小值大的,所以要大于min
舞台中央的我 2013-01-27
  • 打赏
  • 举报
回复
引用 2 楼 szm341 的回复:
select 序号,姓名 from tb t where exists( select 1 from tb where t.姓名=姓名 group by 姓名 having count(*)>1 and min(序号)<t.序号 )
这个 正确 吧 先学习了 一直 不会用having
一枚小菜 2013-01-27
  • 打赏
  • 举报
回复
引用 2 楼 szm341 的回复:
select 序号,姓名 from tb t where exists( select 1 from tb where t.姓名=姓名 group by 姓名 having count(*)>1 and min(序号)<t.序号 )
and min(序号)<t.序号 应该是 and max(序号)<t.序号吧?
Andy-W 2013-01-23
  • 打赏
  • 举报
回复
--姓名重复的数据: 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.序号)
Andy-W 2013-01-23
  • 打赏
  • 举报
回复
--联系代码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.序号)
szm341 2013-01-23
  • 打赏
  • 举报
回复
select 序号,姓名 from tb t where exists( select 1 from tb where t.姓名=姓名 group by 姓名 having count(*)>1 and min(序号)<t.序号 )
专注or全面 2013-01-23
  • 打赏
  • 举报
回复
group * by having count(1)>1

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧