with delegate as (
select DISTINCT(TX_CALLER_ADDRESS) as address_delegate, min (block_timestamp) as date_delegate
from osmosis.core.fact_staking
where action = 'delegate'
and TX_STATUS = 'SUCCEEDED'
group by 1
),
undelegate as (
select DISTINCT(TX_CALLER_ADDRESS) as address_undelegate, min (BLOCK_TIMESTAMP) as date_undelegate
from osmosis.core.fact_staking
where action = 'undelegate'
and TX_STATUS = 'SUCCEEDED'
group by 1
)
SELECT DATEDIFF(day,date_delegate , date_undelegate) AS unstake_Day , count(DISTINCT(address_undelegate)) as Number_wallets
from undelegate join delegate on address_undelegate = address_delegate
group by 1
order by 2 desc