分組后按時間排序每組取最新一條記錄


如圖,我想是實現按取每一種幣別的最新的那條匯率;

 

先按幣別分組 篩選出你小於指定日期的最大時間和幣別集合

select cur_Id,max(pre_time)as preTime from fc_excrate_adjust fe where fe.pre_time<=to_date('2020-05-11', 'yyyy-MM-dd')  group by cur_Id

在用原表內連接 用條件聯立

select a.cur_id,a.old_exchange_rate,a.new_exchange_rate from fc_excrate_adjust a
inner join (select cur_Id,max(pre_time)as preTime from fc_excrate_adjust fe where fe.pre_time<=to_date('2020-05-11', 'yyyy-MM-dd')
group by cur_Id)bs on bs.cur_Id=a.cur_id and bs.preTime = a.pre_time;

這樣獲取的集合即是每個幣種在指定日期前最新匯率的集合了

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM