How to delete rows in a Teradata table that are not in another table?

What makes my situation difficult is that I don’t have a single column with a simple list of primary keys to delete (for example, “delete from the table where the key is located ([list]”). I have several columns together as the primary key and you will need to join them.

Using what I know about other databases, I thought it could be done like:

DELETE FROM
    table1 t1
  LEFT OUTER JOIN
      table2 t2
    ON
      t2.key1 = t1.key1 AND
      t2.key2 = t1.key2
  WHERE
    t2.key1 IS NULL;

But Teradata (v12) responds with error number 3706, saying: "Syntax error: input tables are not allowed in the FROM table."

+3
source share
3 answers

Found this to be done:

DELETE FROM
    table1
  WHERE
    (key1, key2) NOT IN (
      SELECT UNIQUE key1, key2 FROM table2
    );
+7
source

- :

Delete From Table1
Where Not Exists(
                Select 1 
                From Table2 
                Where Table2.key1 = Table1.key1
                    And Table2.key2 = Table1.key2
                )
+2

, ,

The Del_from_me --- table, from which we need to delete the condition_table-- table, to which we will join

Delete dbname.del_from_me   
from     dbname.condition_table CT  
where   dbname.del_from_me.key1 in ('I','N')
and CT.key2=dbname.del_from_me.key2
and CT.key3=dbname.del_from_me.key3
0
source

Source: https://habr.com/ru/post/1777846/


All Articles