updating dup row

From: Patrick B <patrickbakerbr(at)gmail(dot)com>
To: pgsql-general <pgsql-general(at)postgresql(dot)org>
Subject: updating dup row
Date: 2017-02-17 02:25:57
Message-ID: CAJNY3ivTvSZm2=9DX8oAbqbvfqcBwKKota2b1w_dv2jpGqRz_g@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hi all,

how can I update a row with newest id from another table if it exists
somewhere else?

Example:

*table test1*

- id (primary key)
- id_user_bill
- clientid

*table test2*

- item_id
- userid (there are duplicated rows here)
- clientid
- id (primary key)

-- finding the dup records
INSERT INTO test2_results

SELECT

item_id,

userid

count(*) as dup_count,

MAX(id) as recent_id

FROM

test2

GROUP BY

item_id,

userid

HAVING COUNT(*) > 1;

if test1.id_user_bill = test2.id, then
update test1.id_user_bill with test2_results.recent_id

I'm using PG 9.2

Thanks!
Patrick.

Responses

Browse pgsql-general by date

  From Date Subject
Next Message John R Pierce 2017-02-17 02:35:39 Re: updating dup row
Previous Message Richard Brosnahan 2017-02-17 00:39:14 PostgreSQL mirroring from RPM install to RPM install-revisited