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

oracle 指定字段排序并去重?

oracle 指定字段排序并去重?

GCT1015 2018-07-22 14:18:05
有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         |    10在mysql下,可以通过以下SQL获得。SELECT * FROM (SELECT * FROM `logs` ORDER BY `order` DESC) AS a GROUP BY a.userid;但是oracle这么写语法就报错,请问oracle该如何实现?
查看完整描述

3 回答

?
慕娘9325324

TA贡献1783条经验 获得超4个赞

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-07-25
?
撒科打诨

TA贡献1934条经验 获得超2个赞

用开窗函数就可以呀:

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

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


查看完整回答
反对 回复 2018-07-25
  • 3 回答
  • 0 关注
  • 1695 浏览
慕课专栏
更多

添加回答

举报

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