select sum (datediff('day', min_date, max_date)) / sum(TX_Count) as Average_time_from_stake_to_unstake
from (select tx_caller_address,
Min(date_trunc('day', block_timestamp)) as min_date,
Max(date_trunc('day', block_timestamp)) as max_date,
count (distinct tx_id) as TX_Count
from osmosis.core.fact_staking
where tx_status = 'SUCCEEDED'
and currency = 'uosmo'
and action = 'undelegate'
group by 1
)