OneDataAnalystFirst transaction after undelegate
    Updated 2022-07-17
    WITH t1 AS (
    SELECT DELEGATOR_ADDRESS, COMPLETION_TIME
    FROM osmosis.core.fact_staking
    WHERE ACTION = 'undelegate'
    ),

    t2 AS(
    SELECT *
    FROM osmosis.core.fact_transactions
    JOIN t1 ON t1.DELEGATOR_ADDRESS = osmosis.core.fact_transactions.TX_FROM
    WHERE BLOCK_TIMESTAMP >= COMPLETION_TIME
    ),

    t3 AS (
    SELECT MIN(BLOCK_TIMESTAMP) AS First_tx_date , TX_FROM as from_ad
    FROM t2
    GROUP BY 2 ),

    t4 AS( -- TX_ID of first transaction after unstaking completion
    SELECT TX_ID AS TX
    FROM osmosis.core.fact_transactions
    JOIN t3 ON t3.FIRST_TX_DATE = osmosis.core.fact_transactions.block_timestamp AND t3.from_ad=osmosis.core.fact_transactions.tx_from
    ),

    t5 AS(
    SELECT BLOCK_TIMESTAMP, TX_ID, ACTION, 'LP Action' AS Action_type FROM osmosis.core.fact_liquidity_provider_actions UNION ALL
    SELECT BLOCK_TIMESTAMP, TX_ID, ACTION, 'Staking Action' FROM osmosis.core.fact_staking UNION ALL
    SELECT BLOCK_TIMESTAMP, TX_ID, 'Swapping Action', 'Swapping Action' FROM osmosis.core.fact_swaps UNION ALL
    SELECT BLOCK_TIMESTAMP, TX_ID, IFF(TRANSFER_TYPE='OSMOSIS','Transfer OSMO',TRANSFER_TYPE) , 'Transfer' FROM osmosis.core.fact_transfers
    )
    Select Date_trunc('month',BLOCK_TIMESTAMP) AS Month, ACTION, Action_type, Count(*)
    FROM t4
    LEFT JOIN t5 ON t5.TX_ID = t4.tx
    WHERE ACTION IS NOT NULL
    GROUP BY 1,2,3

    Run a query to Download Data