为了账号安全,请及时绑定邮箱和手机立即绑定

oracle 指定字段排序并去重?

oracle 指定字段排序并去重?

三国纷争 2018-08-14 11:09:17
有logs表的结构为uesrid | status    |    order111    | 5         |    7111    | 0         |    9111    | 1         |    8222    | 3         |    3222    | 1         |    2222    | 2         |    4333    | 3         |    10请问如何先以order desc排序,并通过userid去重,同时获取userid,status,order的值。想要的结果为uesrid | status    |    order111    | 0         |    9222    | 2         |    4333    | 3         |    10SELECT * FROM (SELECT * FROM `logs` ORDER BY `order` DESC) AS a GROUP BY a.userid;在mysql下,可以通过以下SQL获得。但是oracle这么写语法就报错,请问oracle该如何实现?
查看完整描述

2 回答

?
交互式爱情

TA贡献1712条经验 获得超3个赞

WITH temp as

    (

    select '111' as userid,'5' as status,'7' as ord from dual

    union all

    select '111' as userid,'0' as status,'9' as ord from dual

    union all

    select '111' as userid,'1' as status,'8' as ord from dual

    union all

    select '222' as userid,'3' as status,'3' as ord from dual

    union all

    select '222' as userid,'1' as status,'2' as ord from dual

    union all

    select '222' as userid,'2' as status,'4' as ord from dual

    union all

    select '333' as userid,'3' as status,'10' as ord from dual

    )

select userid,status,ord from (    

select userid,status,ord,row_number() over(partition by userid order by ord desc)  rn 

from temp

)

where rn=1

;


查看完整回答
反对 回复 2018-08-26
?
函数式编程

TA贡献1807条经验 获得超9个赞

用开窗函数就可以呀:

https://img1.sycdn.imooc.com//5b8241410001f46603160191.jpg

https://img1.sycdn.imooc.com//5b824141000105ba07840379.jpg


查看完整回答
反对 回复 2018-08-26
  • 2 回答
  • 0 关注
  • 1054 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信