Afonso_DiazRetention Rate
    Updated 2024-08-05
    with pricet as (
    select
    hour::date as date,
    avg(price) as price_usd
    from near.price.ez_prices_hourly
    where token_address = 'wrap.near'
    group by 1
    ),

    main as (
    select
    tx_hash,
    block_timestamp,
    signer_id as user,
    amount,
    action,
    amount * price_usd as amount_usd
    from near.gov.fact_staking_actions
    left join pricet on date = block_timestamp::date
    where action = 'staking'
    ),

    user_activity AS (
    SELECT
    user,
    date_trunc('month', block_timestamp) AS date_start
    FROM main
    GROUP BY user, date_start
    )

    SELECT
    a.date_start AS start_date,
    COUNT(DISTINCT a.user) AS active_users,
    COUNT(DISTINCT b.user) AS retained_users,
    (COUNT(DISTINCT b.user) * 100.0 / COUNT(DISTINCT a.user)) AS retention_rate
    FROM user_activity a
    QueryRunArchived: QueryRun has been archived