怎么用sql找最近使用的2个密码?

charlesxu 2016-09-01 05:00:34
User表结构如下:

ID PassWord CreatedDate
002 aaa 2016/06/04
002 bbb 2016/06/03
002 bbb 2016/06/02
002 ccc 2016/06/01

如果用户002的当前密码是aaa,那么2016/06/04的密码与当前密码相同跳过,2016/06/03的密码bbb与当前密码不同,所以选中,2016/06/02的密码与当前密码不同但是与已选密码bbb相同跳过,2016/06/01的密码和当前密码与已选密码bbb都不同,所以选中。

最后该用户最近3次使用的密码是aaa,bbb,ccc
...全文
134 2 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
卖水果的net 2016-09-01
  • 打赏
  • 举报
回复

-- 借楼上数据 
with t as (
select '002' as id,'aaa' as password,'2016/06/04' as createdate from dual
union all
select '002','bbb','2016/06/03' from dual
union all
select '002','bbb','2016/06/02' from dual
union all
select '002','ccc','2016/06/01' from dual
),
m as (
  select id, password, createdate, 
         row_number() over(partition by password order by createdate desc) rn
    from t 
)
select * from m where rownum <= 3 and rn =1 

ghx287524027 2016-09-01
  • 打赏
  • 举报
回复
with t as (
select '002' as id,'aaa' as password,'2016/06/04' as createdate from dual
union all
select '002','bbb','2016/06/03' from dual
union all
select '002','bbb','2016/06/02' from dual
union all
select '002','ccc','2016/06/01' from dual
)
select id,password,max(createdate) 
from t 
where password='aaa' 
group by id,password
union all
select id,password,max(createdate) 
from t 
where password<>'aaa'
group by id,password
order by 1,3 desc,2 

17,140

社区成员

发帖
与我相关
我的任务
社区描述
Oracle开发相关技术讨论
社区管理员
  • 开发
  • Lucifer三思而后行
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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