SQL - SELECT DISTINCT in only one field

Asked

Viewed 2,432 times

1

So, I’m doing a search in which returns the non-repeating geometries (which has as layer name Access and Middle):

SELECT DISTINCT geo_geometria
FROM geral_elemento_cartografico
WHERE ds_camada LIKE ('%MEIO%') OR ds_camada LIKE ('%ACESSO%')

however, I would like it to appear next to the return, the column ds_camada also (like the following query), as if distinct acted only in geo_geometry, since the contents of the layers are repeated

SELECT DISTINCT geo_geometria, ds_camada
FROM geral_elemento_cartografico
WHERE ds_camada LIKE ('%MEIO%') OR ds_camada LIKE ('%ACESSO%')

however, from 4533 returns he now catches 5602 returns

someone knows how to solve?

1 answer

1


Postgresql has the DISTINCT ON clause. For your case would be:

SELECT DISTINCT ON(geo_geometria) geo_geometria, ds_camada
FROM geral_elemento_cartografico
WHERE ds_camada LIKE ('%MEIO%') OR ds_camada LIKE ('%ACESSO%')

Browser other questions tagged

You are not signed in. Login or sign up in order to post.