sql - pgSQL to overwrite one set of table rows with another based on ID -
i have table along lines of -
set_id, id, ... 0, 1, a, b, ... 1, 1, c, d, ... 2, 1, e, f, ... 0, 2, g, h, ... 1, 2, i, j, ... 2, 2, k, l, ...
where (set_id, id) primary key
i replace fields except set_id entries set_id 0 equivalent entry (if exists) set_id 1. table above become -
set_id, id, ... 0, 1, c, d, ... < 1, 1, c, d, ... 2, 1, e, f, ... 0, 2, i, j, ... < 1, 2, i, j, ... 2, 2, k, l, ...
is there sane way in sql or need utilize client side code?
thanks
if i'm reading correctly, want re-create info in row set_id=1
row set_id=0
each value of id
.
one way of doing simple update
from
;
update mytable set field_a = ms.field_a, field_b = ms.field_b mytable ms mytable.id=ms.id , mytable.set_id=0 , ms.set_id=1
note need list fields want moved, "row copy" not automatic fields.
an sqlfiddle test with.
sql database postgresql
No comments:
Post a Comment