carlesmontalanakamoto osmosis2
    Updated 2022-11-03
    --credits to adriaparcerisas

    WITH
    stakes1 as (
    SELECT
    block_timestamp,
    tx_id,
    REDELEGATE_SOURCE_VALIDATOR_ADDRESS as validator,
    (amount / power(10,6))*(-1) as staked
    FROM osmosis.core.fact_staking
    where currency LIKE 'uosmo'
    AND REDELEGATE_SOURCE_VALIDATOR_ADDRESS is not null
    ),
    stakes2 as (
    SELECT
    block_timestamp,
    tx_id,
    validator_address as validator,
    CASE WHEN action = 'undelegate' then (amount / power(10,6))*(-1)
    else amount/power(10,6) end as staked
    FROM osmosis.core.fact_staking
    where currency LIKE 'uosmo'
    AND REDELEGATE_SOURCE_VALIDATOR_ADDRESS is NULL
    ),
    stakes3 as (
    SELECT * FROM stakes1 UNION SELECT * FROM stakes2
    ),
    monthly as (
    SELECT
    trunc(block_timestamp,'day') as days,
    tx_id,
    validator,
    sum(staked) as osmo_staked
    FROM stakes3 WHERE staked is not null group by 1,2,3
    ),
    totals as (
    Run a query to Download Data