delete from ttracker_options where option_id = :option_id and not exists (select tt.ticket_id from ttracker_options ts, ttracker_tickets tt, ttracker_categories tc where ts.option_id = :option_id and tt.category_id = tc.category_id and tc.package_id = ts.package_id and ((ts.function = 'severity' and ts.value = tt.severity) or (ts.function = 'priority' and ts.value = tt.priority)))