Re: how to update table to make dup values distinct

From: Harald Fuchs <hf0923x(at)protecting(dot)net>
To: pgsql-sql(at)postgresql(dot)org
Subject: Re: how to update table to make dup values distinct
Date: 2005-11-11 14:38:49
Message-ID: 871x1n5jwm.fsf@srv.protecting.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

In article <20051110105818(dot)4dc51e8c(dot)gry(at)ll(dot)mit(dot)edu>,
george young <gry(at)ll(dot)mit(dot)edu> writes:

> [PostgreSQL 7.4RC2 on i686-pc-linux-gnu](I know, I know... must upgrade soon)
> I have a table mytable like:
> i | txt
> ---+-------
> 1 | the
> 2 | the
> 3 | rain
> 4 | in
> 5 | mainly
> 6 | spain
> 7 | stays
> 8 | mainly
> 9 | in

> I want to update it, adding a ':' to txt so that each txt value is unique.
> I don't care which entry gets changed. I tried:

> update mytable set txt=mytable.txt || ':' from mytable t2 where mytable.txt=t2.txt and mytable.i=t2.i;

> but this updated both duplicated entries.

> Um, there may sometimes be 3 or 4 duplicates, not just two. For these, I can add multiple colons, or one each of an assortment of characters, say ':+*&^#'.

> Performance does not matter here. The real table has 30K rows, ~200 dups.
> To clarify, I want to end up with something like:

> 1 | the
> 2 | the:
> 3 | rain
> 4 | in
> 5 | mainly:
> 6 | spain
> 7 | stays
> 8 | mainly
> 9 | in:

Try the following:

UPDATE mytable
SET txt = txt || substring ('::::::::::::::::' for (
SELECT count(*)
FROM mytable t1
WHERE t1.txt = mytable.txt AND t1.i < mytable.i
)::int)

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message A. Kretschmer 2005-11-11 16:09:21 Re: cli in sql?
Previous Message Joel Fradkin 2005-11-11 14:09:55 Re: High level discussion for design of using ACL to retrieve Data