Subject Re: [firebird-support] deleting duplicate records
Author Daniel Berstein
That delete statement makes little sense. If your subquery can "identify" the desired
rows to be deleted you could simply use:

DELETE FROM table
WHERE IDNUM = :id
AND STCKNUM = :stknum
AND QTY = :qt

I see no simple solution for deleting duplicate rows where all columns have the same
value. The only approach I can imagine is something like:

1. First detect duplicate rows with something like:

SELECT col1, col2, .. , colN, Count(*)
FROM table
GROUP BY col1, col2, .. , colN
HAVING Count(*) > 1
INTO :val_col1, :val_col2, .. , :val_colN

2. Loop the above result set and delete any record of the duplicate tuple values using
the RDB$DB_KEY value:

SELECT FIRST 1 RDB$DB_KEY
FROM table
WHERE col1 = :val_col1
AND col2 = :val_col2
...
AND colN = :val_colN
INTO :val_dbkey

DELETE FROM table
WHERE RDB$DB_KEY = :val_dbkey

3. Repeat this procedure until query (1) returns no results.



Regards,
Daniel Berstein.

On 29 Aug 2003 at 19:14, Dixon Epperson wrote:

> The table I am working with cannot have any restraints or key fields
> because there are times when a duplicate record may be desired. I
> know I could put an index number on each record and get rid of
> unwanted duplicates that way but is there a way to do something like
> this?
>
> DELETE FROM TABLE WHERE $RDB_RECNUMBER=(SELECT $RDB_RECNUMBER FROM
> TABLE WHERE IDNUM=:id AND STCKNUM=:stknum AND QTY=:qt)
>
> Dixon Epperson
>
>
>
> To unsubscribe from this group, send an email to:
> firebird-support-unsubscribe@yahoogroups.com
>
>
>
> Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
>
>
>