我是 sql 查詢的新手,有一個我無法處理的案例。假設我有一個如下表;
id | document_name | country | major_version | minor_version |
1 | policy1 | DE | 1 | 0 |
2 | policy2 | DE | 1 | 0 |
3 | policy1 | DE | 1 | 1 |
4 | policy1 | DE | 2 | 0 |
5 | policy2 | DE | 1 | 1 |
6 | policy2 | IT | 1 | 0 |
7 | policy2 | IT | 1 | 1 |
我只想回傳;
id | document_name | country | major_version | minor_version |
4 | policy1 | DE | 2 | 0 |
5 | policy2 | DE | 1 | 1
|
國家/地區為“DE”的檔案的最新版本。我應該運行什么樣的查詢?
我嘗試過,distinct on document_name但它沒有回傳最新版本的檔案。
uj5u.com熱心網友回復:
我嘗試過,
distinct on document_name但它沒有回傳最新版本的檔案。
distinct onorder by如果您使用正確的子句,在 Postgres 中是一個很好的方法:
select distinct on (document_name) t.*
from mytable t
order by document_name, major_version desc, minor_version desc
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/526895.html
下一篇:更新陳述句中的奇怪語法
