![](/img/trans.png)
[英]How to get the latest records of the multiple values of same column in PostgreSQL?
[英]How to fetch the latest records in PostgreSQL?
實際上我正在嘗試通過r_id
和最新的start_date
獲取這些數據的最新記錄
我的樣本數據:
**r_id** **s_id ** **start_date**
"149165" "28317" "2020-09-07 08:12:46.108"
"149165" "28324" "2020-09-07 08:18:15.934"
"149161" "28313" "2020-09-07 08:11:33.923"
"149161" "28316" "2020-09-07 08:12:19.887"
"149161" "28312" "2020-09-07 08:11:04.448"
"149161" "28305" "2020-09-07 08:09:17.211"
預期成績 :
**r_id** **s_id ** **start_date**
"149165" "28324" "2020-09-07 08:18:15.934"
"149161" "28316" "2020-09-07 08:12:19.887"
任何人請。
您可以使用distinct on
:
select distinct on (r_id) t.*
from mytable t
order by r_id, start_date desc
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.