I am trying to write a method that removes duplicates from tables, without having to know the details of the table for generality (i.e., it should run on any table).
I am using the following method from here (last method) through psycopg2
:
CREATE TABLE tempTable (LIKE "{table}");
INSERT INTO tempTable(*)
SELECT
DISTINCT ON ("{column}") *
FROM "{table}";
DROP TABLE "{table}";
ALTER TABLE tempTable
RENAME TO "{table}";
DROP table tempTable
The problem is that you can't use * to get all columns inside the INSERT INTO
command. We are expected to specify a list of all the columns to insert, which is probably good for control, but is bad for my purpose here.
I know I can get the list of column names for the table with
SELECT column_name FROM information_schema.columns WHERE table_name = "{table}"
but I can't just plug that as a subquery in place of the *
.
Answers to this 8 year old question state that it's just not possible in SQL. That might still be true, or it might not be true.
Can this be done now?