4

I want to remove duplicate rows return from a SELECT Query in Postgres

I have the following query

SELECT DISTINCT name FROM names ORDER BY name

But this somehow does not eliminate duplicate rows?

3
  • I'm using an ancient PostgreSQL 7.3.4 version Commented Jul 20, 2009 at 8:30
  • possible duplicate of Remove duplicate from a table Commented Mar 4, 2014 at 20:32
  • It's not a duplicate. @Flimzy ... a different question, with a fairly vague title. Commented Mar 29, 2015 at 11:17

4 Answers 4

10

PostgreSQL is case sensitive, this might be a problem here DISTINCT ON can be used for case-insensitive search (tested on 7.4)

SELECT DISTINCT ON (upper(name)) name FROM names ORDER BY upper(name);
Sign up to request clarification or add additional context in comments.

Comments

1

Maybe something with same-looking-but-different characters (like LATIN 'a'/CYRILLIC 'а')

Comments

1

Don't forget to add a trim() on that too. Or else 'Record' and 'Record ' will be treated as separate entities. That ended up hurting me at first, I had to update my query to:

SELECT DISTINCT ON (upper(trim(name))) name FROM names ORDER BY upper(trim(name));

Comments

1

In Postgres 9.2 and greater you can now cast the column to a CITEXT type or even make the column that so you don't have to cast on select.

SELECT DISTINCT name::citext FROM names ORDER BY name::citext

Comments

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.