delete from the main table records that appear in the cte

Consider below approach

delete from main_table mt
where exists (
  with users_to_exclude as (
    select uid, date, location_id
    from some_previous_ctes_with_logic
  )
  select 1 from users_to_exclude ux
  where ux.uid = mt.uid
  and ux.date = mt.date
  and ux.location_id = mt.location_id
);    

why do I have to attach it to the main table in the where clause?
If for some reason you want to go this direction – use below

delete from main_table mt
where (uid, date, location_id) in (
  with users_to_exclude as (
    select uid, date, location_id
    from some_previous_ctes_with_logic
  )
  select as struct  * from users_to_exclude
);

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top