SELECT DISTINCT …
removes duplicate rows. Is there any way to remove duplicate columns, that is, columns where every value is the same? I'm looking through a legacy schema for the "distinguishing features" of some rows, so this would be super helpful. If it makes it any easier all of the columns are simple types like INT
, TEXT
and TIMESTAMP
. Basically, given a table like this:
CREATE TEMPORARY TABLE column_test
(
foo TEXT,
bar INT,
baz BOOLEAN,
bat TIMESTAMP WITH TIME ZONE
);
INSERT INTO column_test (
foo, bar, baz, bat
) VALUES
('lorem ipsum', 1, TRUE, '2000-01-01 UTC'),
('lorem ipsum', 2, TRUE, '2000-01-01 UTC'),
('other', 3, TRUE, '2000-01-01 UTC');
is it possible to write a query which will select only the foo
and bar
columns, since those are the only columns with more than one value? Something like this:
SELECT columns_with_more_than_one_value(*) FROM column_test;
foo bar
'lorem ipsum' 1
'lorem ipsum' 2
'other' 3
I guess one way would be to SELECT *
, transpose the result, remove any rows where all the values are the same, then transpose again, but that would be very complex and I don't know how I would keep the original column names in the result.