Search code examples
postgresqlselectselect-into

PostgreSQL: Select several queries into same table


Good day,

I have a table with some records that should be deleted. I would like to keep track of the deleted records and place such records in a new table. I would like to do the following:

SELECT * INTO TEMP FROM TABLE WHERE criteria < 1;

And then delete these records with DELETE query. Later on I would like to do a new SELECT query:

SELECT * INTO TEMP FROM TABLE WHERE new_criteria > 2;

And then delete those records as well. I will be working from only one table and just place the selected records into the same, new table (just for reference).

Thanks!


Solution

  • INSERT INTO temp (SELECT * FROM tbl WHERE criteria < 1);