數據庫中授信表字段如下:
授信時間(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