2

I've got, in PostgreSQL/PostGIS,

create table datos (
gbifID int primary key,
orden varchar (50),
eventDate varchar (50),
geom geometry);

When I run

select distinct (geom,eventdate), geom
from datos
where orden='x';

it works OK. However,

create view esfuerzo as 
select distinct (geom,eventdate), geom
from datos
where orden='x';

returns ERROR: column "row" has pseudo-type record Estado SQL: 42P16. How can I create such a view?

1
  • 1
    The correct syntax is SELECT DISTINCT ON (geom, eventdate) geom, eventdate FROM datos WHERE orden='x'. By omitting on, Postgres thinks you are looking for a composite type, rather than essentially just a GROUP BY geom, evendate. Commented Jul 3, 2019 at 15:28

3 Answers 3

3

As @IanTurton pointed out, enclosing (multiple) column names in parenthesis will invoke a cast to RECORD.

Simply run

CREATE OR REPLACE VIEW esfuerzo AS
  SELECT DISTINCT
         eventdate,
         geom
  FROM   datos
  WHERE  orden = 'x'
;

or possibly better, the equivalent

CREATE OR REPLACE VIEW esfuerzo AS
  SELECT geom
  FROM   datos
  WHERE  orden = 'x'
  GROUP BY
         eventdate, geom
;

where you can choose the columns to select from the list passed to GROUP BY.

1

You can't use a pseudo-type as a column type. Since geom and eventdate are different types I suspect you get a sort of undefined type in the results. You may be able to cast it to something specific or could maybe try:

create view esfuerzo as 
select distinct (st_astext(geom),eventdate), geom
from datos
where orden='x';

to make the result a varchar.

4
  • Same error result with st_astext Commented Jul 3, 2019 at 9:21
  • Actually, I only need that "distinct (geom,eventdate)" as a filter, not the actual results. Perhaps it could be specified somehow within the WHERE conditions? Commented Jul 3, 2019 at 9:57
  • 1
    if you really need a composite type in a View you can cast the implicit record to a type both values can safely be cast to. e.g. TEXT: CREATE VIEW esfuerzo AS SELECT (geom, eventdate)::TEXT ... (which will be translated into ... ROW(geom, eventdate)::TEXT as "row" ...). Or, better, create a specific type to be able to keep the column types. Commented Jul 3, 2019 at 11:22
  • 1
    I suspect the OP wasn't actually looking for a composite type but simply a DISTINCT ON (x,y) type query. The question is lacking a certain amount of detail, it is true. Commented Jul 3, 2019 at 15:31
1

The correct syntax is SELECT DISTINCT ON (geom, eventdate) geom, eventdate FROM datos WHERE orden='x'. By omitting on, Postgres thinks you are looking for a composite type, rather than essentially just a GROUP BY geom, evendate. – John Powell Jul 3 at 15:28

1
  • This is exactly the right solution for me. Have my solitary upvote Commented Apr 2, 2023 at 20:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.