3

I'm trying to create a unique index on code field:

$ CREATE UNIQUE INDEX "one_code_per_person" on "core_person"("code") WHERE "code" IS NOT NULL;
ERROR:  could not create unique index "one_code_per_person"
DETAIL:  Table contains duplicated values.

It says there are duplicate values but:

$ select code, count(*) from core_person group by code having count(*)>2;
code | count 
-----------+-------
(0 rows)
  • which proves there are not.

WTF?

1
  • 3
    Actually, count(*)>1 would be the condition you need to test. Commented Jan 18, 2011 at 21:14

1 Answer 1

10

This:

HAVING COUNT(*) > 2

...means there needs to be 3+ duplicates. You want to use:

HAVING COUNT(*) > 1

...to find records with 2+ duplicates.

Sign up to request clarification or add additional context in comments.

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.