Question

Suppose I have a table as follows: (on DB2 9.7.2)

       COL1            COL2             COL3
-----------      ----------       ----------
          3               4              xyz
          3               4              xyz

Now I want to write a query such that only one from these two identical records will be deleted. How can I achieve this?

I can think of : delete from ;

or

delete from where col1=3;

but both of the above queries will delete both records whereas I want to keep one of them.

Was it helpful?

Solution

If LIMIT doesn't work, this will:

DELETE FROM (SELECT * FROM tbl WHERE col = 3 FETCH FIRST ROW ONLY)

OTHER TIPS

Can't you use a limit clause?

DELETE FROM <table> WHERE <column>=3 LIMIT 1

This is something that served my purpose:

DELETE FROM tabA M 
WHERE  M.tabAky IN (SELECT tabAky 
                    FROM   (SELECT tabAky, 
                                   ROW_NUMBER() OVER (PARTITION BY tabAcol1,
                                                                   tabAcol2,
                                                                   tabAcoln)
                            FROM   tabA a) AS X (tabAky, ROWNUM) 
                    WHERE ROWNUM> 1) ; 

Try This

delete from table A (select row_number() over (partition by col1 order by col1 ) count,* from table) where A.count> 1
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top