2017-07-29 7 views
0

私はPostgres 9.5を使用しています。私はいくつかの列を持つテーブルを持っています...Postgresでは、テーブルから最近の日付の行を選択するにはどうすればよいですか?

crypto_currency_id | integer      | 
price    | integer      | 
last_updated  | timestamp without time zone | 

crypto_currency_idには複数のエントリがあります。私の質問は、どのように私はテーブル内の各一意のcrypto_currency_idの最新のエントリだけを選択するのですか?だから、例えば、私のテーブルは以下のようになり、私は2つの行を返すようにクエリを望む

crypto_currency_id  price   last_updated 
===================================================== 
2       50    2017-06-01 
2       52    2017-07-01 
3       500   2017-01-01 

エントリ

を、含まれている場合

2       52    2017-07-01 
3       500   2017-01-01 
+0

[最終日に基づいてレコードを選択](https://stackoverflow.com/questions/19027881/select-records-based-on-last-date)の可能な複製 – Abelisto

答えて

0

Postgresの中で最も効率的な方法がdistinct onです:

select distinct on (crypto_currency_id) t.* 
from t 
order by crypto_currency_id, last_updated desc; 
関連する問題