I want to create a table from a SELECT statement based on a multiple row DISTINCT, but PostgreSQL does not allow me to do it.
I need to create a table where the elements were previously selected from another table and they are all unique.
Here's my try:
CREATE TABLE new_name AS (
SELECT DISTINCT(table.field1, table.field2), table.field1, table.field2,
FROM ...
WHERE ...
);
If I run this statement, PostgreSQL says:
ERROR: column "row" has pseudo-type record
How could I solve this problem?
Best Answer
distinct
is NOT a function. It always operates on all columns of the result.The difference between
select distinct (foo), bar
andselect distinct foo, bar
is the same as the difference betweenselect (foo), bar
andselect foo, bar
. The parenthesis is just "noise".When you write
select (foo,bar)
you are actually creating an anonymous record type in Postgres which results in a single column that has two attributes - which is not what you actually want.As you are using Postgres, you can use the (proprietary) operator
DISTINCT ON
which - in contrast to the standardDISTINCT
- does operate on a sub-set of the columns.You have to specify an ORDER BY in that case to define which of the rows to take if there is more than one with the same combination of
(field1, field2)
.If you want to stick to ANSI SQL you will need a window function for this:
For a large table,
DISTINCT ON
is probably faster than the solution with the window function.