Created
April 12, 2020 20:27
-
-
Save all4miller/c39d0159b3da72c1f004cab952aecad9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
create table table_data ( | |
code integer primary key not null, | |
data_value varchar(30) not null | |
); | |
create table stage_data ( | |
id serial primary key, | |
code integer not null, | |
data_value varchar(30) not null | |
); | |
insert into stage_data (code, data_value) | |
values (1, 'value 1'), | |
(2, 'value 2'), | |
(3, 'value 3'), | |
(3, 'value 3 xx'); | |
with ranked as ( | |
select | |
code, | |
data_value, | |
rank() over (partition by code order by id desc) id_rank | |
from stage_data | |
) | |
insert into table_data (code, data_value) | |
select code, data_value | |
from ranked | |
where id_rank = 1 | |
on conflict (code) | |
do | |
update | |
set data_value = excluded.data_value; | |
select * | |
from table_data; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment