2017-01-10 1 views
0

私は以下のようなテーブルを持っている:行の等価性をチェックし、postgrsqlの別の列を更新する方法は?

enter image description here

あなたは、「トランザクションID」欄には多くのエントリに同じ値が含まれ、上から見ることができるように。達成する必要があるのは、同じ "transaction-id"を持つすべてのエントリに対して、同じ "rank_value"が必要なことです。

必要な結果を以下に示します。 enter image description here

あなたが元の表の空の値が後で1中で埋めている見ることができるように。私はpostgresqlを使ってこれをどのように達成できますか?

+0

http://meta.stackoverflow.com/questions/285551/why-may-i-not-upload-images-of-code-on-so-when-asking-a-question/285557#285557 –

答えて

0

1行が値を持っている場合、あなただけのウィンドウ関数としてmax()を使用することができます。

select t.*, max(rank_value) over (partition by transaction_id) 
from t; 

あなたはPostgresの中updateでこれを実行する場合は、あなたが行うことができます:

update t 
    set rank_value = tt.rank_value 
    from (select transaction_id, max(rank_value) as rank_value 
      from t 
      group by transaction_id 
     ) tt 
    where t.transaction_id = tt.transaction_id; 
関連する問題