-- Sélect the run_ids for a specific definition_key
select eru.run_id 
from det.execution_run eru 
where eru.definition_key = 'azure.sponsorship.focus.daily' 
and date(start_time) = '2026-08-14'

-- select the span_id corresponding to the run_ids
select esp.span_id from det.execution_span esp where esp.run_id in (
    select eru.run_id from det.execution_run eru 
    where eru.definition_key = 'azure.sponsorship.focus.daily' 
    and date(start_time) = '2026-08-14'
)

-- select the event_id corresponding to the span_ids
select sev.event_id from det.span_event sev where sev.span_id in (
    select esp.span_id from det.execution_span esp where esp.run_id in (
        select eru.run_id from det.execution_run eru 
        where eru.definition_key = 'azure.sponsorship.focus.daily' 
        and date(start_time) = '2026-08-14'
    )
)

-- select the data operations corresponding to the span_id
select dop.data_operation_id from det.data_operation dop where dop.span_id in (
    select esp.span_id from det.execution_span esp where esp.run_id in (
        select eru.run_id from det.execution_run eru 
        where eru.definition_key = 'azure.sponsorship.focus.daily' 
        and date(start_time) = '2026-08-14'
    )
)

-- select the metrics corresponding to the span_id
select mme.metric_id from det.metric_measurement mme where mme.span_id in (
    select esp.span_id from det.execution_span esp where esp.run_id in (
        select eru.run_id from det.execution_run eru 
        where eru.definition_key = 'azure.sponsorship.focus.daily' 
        and date(start_time) = '2026-08-14'
    )
)

-- Purge metric measurement
delete from det.metric_measurement mme where mme.span_id in (
    select esp.span_id from det.execution_span esp where esp.run_id in (
        select eru.run_id from det.execution_run eru 
        where eru.definition_key = 'azure.sponsorship.focus.daily' 
        and date(start_time) = '2026-08-14'
    )
)

-- purge data_operations
delete from det.data_operation dop where dop.span_id in (
    select esp.span_id from det.execution_span esp where esp.run_id in (
        select eru.run_id from det.execution_run eru 
        where eru.definition_key = 'azure.sponsorship.focus.daily' 
        and date(start_time) = '2026-08-14'
    )
)

-- purge span_event
delete from det.span_event sev where sev.span_id in (
    select esp.span_id from det.execution_span esp where esp.run_id in (
        select eru.run_id from det.execution_run eru 
        where eru.definition_key = 'azure.sponsorship.focus.daily' 
        and date(start_time) = '2026-08-14'
    )
)

-- purge execution_span
delete from det.execution_span esp where esp.run_id in (
    select eru.run_id from det.execution_run eru 
    where eru.definition_key = 'azure.sponsorship.focus.daily' 
    and start_time <= '2026-08-21T13:40:00Z'
)

-- purge execution_run
delete
from det.execution_run eru 
where eru.definition_key = 'azure.sponsorship.focus.daily' 
and date(start_time) = '2026-08-14'