postgresqlinsert

postgres insert into same id


I have this table1:

product amount timestamp_local
Prdct_1 100 2022-03-15
Prdct_2 50
Prdct_3 40 2023-05-05

I want to insert into this table2

product amount timestamp_local
Prdct_1 100
Prdct_2 50 2021-12-30
Prdct_3 40

This is my query but it doesn't work.

insert into table2 (timestamp_local)
select timestamp_local from table1 b
where not exists (select 1 from table2 where id = b.id);

How could I solve it? Thanks for your answers.


Solution

  • INSERT INTO table2 (product, amount, timestamp) SELECT product, amount, COALESCE(timestamp, '2021-12-30') AS timestamp FROM table1;