0xHaM-dLorenz Curve - NEAR Validators (Last Month) copy
    Updated 2023-05-09
    -- forked from Lorenz Curve - NEAR Validators (Last Month) @ https://flipsidecrypto.xyz/edit/queries/662f3689-2728-4d6a-a4b1-30f86f817162

    WITH active_set as (
    select
    address as validator,
    balance as power
    from near.core.fact_staking_pool_daily_balances
    where date = current_date - 1
    ORDER by 1 DESC
    LIMIT 100
    ),

    tot_staked as (
    SELECT
    SUM(power) as all_near_staked
    FROM active_set
    ),

    lino2 as (
    SELECT
    validator,
    power as stake,
    all_near_staked,
    ROUND(stake / all_near_staked * 100, 2) as power_percentage,
    SUM(power_percentage) OVER (ORDER BY power) as cumulative_stake_perc
    FROM active_set, tot_staked
    GROUP BY 1, 2, 3
    ORDER BY stake
    LIMIT 100
    )
    SELECT
    validator,
    round(stake, 2) as staked,
    cumulative_stake_perc as "Cumulative Voting Power",
    0.01 * ((cumulative_stake_perc + LAG(cumulative_stake_perc, 1, 0) OVER (ORDER BY staked DESC)) / 100) * 0.5 as lorenz_curve,
    ROW_NUMBER() OVER (ORDER BY staked) as "Perfect Equality"
    Run a query to Download Data