mysql partition by的用法


数据库中授信表字段如下:

授信时间(create_time) 客户来源渠道(dept_id) 客户id(user_id) 客户额度(cash)
2020/3/12 13:04:31 aa001 100036 ¥20,000.00

其中共有100个不同客户来源渠道,每个渠道10000个客户,一共100万条数据。

现在需取出100个渠道,每个渠道的最后授权时间对应的这条记录的四个字段。

select create_time , dept_id, user_id ,cash  
from (select *, rank()over(partition by dept_id order by create_time desc) m 
      from table ) table1
where table1.m = 1
select create_time , dept_id ,user_id, cash
from table
where (create_time,dept_id) in 
	  (select max(create_time),dept_id from table group by dept_id)

参考链接:https://blog.csdn.net/weixin_44547599/article/details/88764558


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM