Search code examples
subquerydelete-rowhana

DELETE FROM (SELECT ...) SAP HANA


How come this does not work and what is a workaround?

    DELETE FROM
    (SELECT
     PKID
    , a
    , b)
    Where a > 1 

There is a Syntax Error at "(".

DELETE FROM (TABLE) where a > 1 gives the same syntax error.

I need to delete specific rows that are flagged using a rank function in my select statement.


Solution

  • I have now put a table immediately after the DELETE FROM and put WHERE restrictions on the DELETE and in a small series of self-joins of the table.

    DELETE FROM TABLE1
    WHERE x IN
        (SELECT A.x
        FROM (SELECT x, r1.y, r2.y, DENSE_RANK() OVER (PARTITION by r1.y, r2.y ORDER by x) AS RANK
            FROM TABLE2 r0
            INNER JOIN TABLE1 r1 on r0.x = r1.x
            INNER JOIN TABLE1 r2 on r0.x = r2.x
            WHERE r1.y = foo and r2.y = bar
            ) AS A
        WHERE A.RANK > 1
        )