Oracle表中重复记录只显示一条

1.Oracle某个表中有重复记录,想重复记录只显示一条。

表中内容如下:

ID                     Name           score

zs40.00
1zs40.00
1zs40.00
2ls80.00
2ls80.00
3ww90.00
3ww90.00
4zl100.00

用rowid写个最容易懂的:

select * from t_temp where rowid in(
select max(rowid) from  t_temp group by id)
order by id;

显示结果:

ID         Name           score

1zs40.00
2ls80.00
3ww90.00
4zl100.00

最好是这么写,显示结果是一样的,效率要高很多:

with x as (
select max(rowid) r from  t_temp group by id)
select t.id,t.name,t.score from t_temp t,x
where t.rowid = x.r
order by t.id;

2. 查询表中的重复记录。

select * from t_temp t,(select id from t_temp group by id having count(1)>1) x
where t.id=x.id
order by t.id

显示结果:

ID            Name           score

zs40.00
1zs40.00
1zs40.00
2ls80.00
2ls80.00
3ww90.00
3ww90.00

3. 有另外一张表:t_temp2

ID             ClassNM

1class1
2class2
3class3
4class4

这里的ID跟上张表的ID是一个概念。

现在要求查询出第二张表中所有的人,按照第一张表的重复出现条数进行排序显示(也就是要看每个人都重复了几次,都是哪个班的)。显示结果如下。

ID                   ClassNM               NUM

1class13
2class12
3class22
4class41

SQL语语如下哦。

with x as(select count(id) num, id from t_temp group by id)
select t2.id,t2.classnm,x.num from t_temp2 t2,x
where t2.id=x.id
order by x.num desc,id asc

 

好记忆赶不上滥笔头,真是的。

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值