
create table bad_ids as
with main_clips as (
  select id from extracted_clips c where exists (
    select from automatic_extracted_clip_tags a
    join tag_values tv on tv.id = a.tag_value_id
    where a.extracted_clip_id = c.id
    and tv.value like 'Trainable as Main'
  )
)
select id from main_clips c
join lateral (
  with onsets as (
    select (jsonb_array_elements(notes)->'start')::real onset
    from extracted_clip_notes cn
    where cn.extracted_clip_id = c.id
    union all select 40 onset
  ), voids as (
    select onset - lag(onset, 1, 0) over (order by onset asc) delta from onsets
  ) select max(delta) maxvoid from voids
) a on true
where a.maxvoid >= 8;
