DELETE语句部分删除数据

时间:2019-06-02 10:56:28

标签: db2 sql-delete

当我在DB2中执行以下查询时,它不会立即删除数据。

我有800条记录,其中每2条记录都是重复的,我想删除2条记录中的1条记录,因此它将在DB中保留400条记录。

以下是RESERVATION_NUMBER的示例。

enter image description here

DELETE 
FROM   reservation_number 
WHERE  reservation_id IN (SELECT reservation_id 
                          FROM   (SELECT ROW_NUMBER() 
                                           OVER() AS RN, 
                                         msr1.reservation_number, 
                                         msr1.reservation_id, 
                                         msr1.used_flag 
                                  FROM   reservation_number msr1, 
                                         reservation_number msr2 
                                  WHERE 
              msr1.reservation_number = msr2.reservation_number 
              AND msr1.reservation_id <> msr2.reservation_id 
                                  ORDER  BY msr1.reservation_number) 
                          WHERE  Mod (rn, 2) = 0 
                          ORDER  BY reservation_number) 

如果我多次执行此查询,它将删除完整的数据。数据将以以下方式删除-

400、168、076、038、019、003、001

2 个答案:

答案 0 :(得分:0)

我知道了。我在OVER()中缺少参数。

这是正确的查询

DELETE
FROM
    RESERVATION_NUMBER
WHERE
    RESERVATION_ID IN (
    SELECT
        RESERVATION_ID
    FROM
        (
        SELECT
            ROW_NUMBER() OVER(ORDER BY msr1.RESERVATION_NUMBER) AS RN,
            msr1.RESERVATION_NUMBER,
            msr1.RESERVATION_ID,
            msr1.USED_FLAG
        FROM
            RESERVATION_NUMBER msr1 ,
            RESERVATION_NUMBER msr2
        WHERE
            msr1.RESERVATION_NUMBER = msr2.RESERVATION_NUMBER
            AND msr1.RESERVATION_ID <> msr2.RESERVATION_ID
        ORDER BY
            msr1.RESERVATION_NUMBER )
    WHERE
        MOD (RN,2)=1 
    ORDER BY
        RESERVATION_NUMBER )

答案 1 :(得分:0)

这会不会容易吗?

Property Key: property[0].name, Property Value: A
Property Key: property[1].name, Property Value: B
Property Key: property[0].value, Property Value: 1
Property Key: property[1].value, Property Value: 2
Property Key: property[2].name, Property Value: C
Property Key: property[2].value, Property Value: 3