Afonso_DiazGrouping balances
    Updated 2024-04-15
    with

    deposit as (
    select user,sum(increase_volume) as increase_volume from (
    select
    to_address as user,
    sum(amount) as increase_volume
    from blast.core.ez_native_transfers
    where tx_hash not in (select distinct tx_hash from blast.core.ez_native_transfers where to_address = origin_from_address)
    group by 1

    union

    select
    to_address as user,
    sum(amount) as increase_volume
    from blast.core.ez_native_transfers
    where to_address = origin_from_address
    group by 1
    )
    group by 1
    ),

    withdraw as (
    select
    from_address as user,
    sum(amount) as decrease_volume
    from blast.core.ez_native_transfers
    where from_address = origin_from_address
    group by 1
    ),

    balances as (
    select
    user,
    increase_volume - nvl(decrease_volume, 0) - nvl((select sum(tx_fee) from blast.core.fact_transactions where from_address = user), 0) as balance_eth
    QueryRunArchived: QueryRun has been archived