hessShare of Profitable Users
    Updated 2023-02-03
    with purchaser as ( select date(block_timestamp) as date, purchaser , mint ,ADDRESS_NAME, sum(sales_amount) as buy, count(DISTINCT(tx_id)) as total_buy
    from solana.core.fact_nft_sales a join solana.core.dim_labels b on a.mint = b.address
    where date between '{{From}}' and '{{To}}'
    group by 1,2,3,4)
    ,
    seller as ( select date(block_timestamp) as date, seller , mint ,ADDRESS_NAME, sum(sales_amount) as sell, count(DISTINCT(tx_id)) as total_sell
    from solana.core.fact_nft_sales a join solana.core.dim_labels b on a.mint = b.address
    where date between '{{From}}' and '{{To}}'
    group by 1,2,3,4)
    ,
    final as ( select purchaser , sum(total_buy) as buy_tx , sum(total_sell) as sell_tx ,
    sum(buy) as buy_amount , sum(sell) as sell_amount, sell_amount-buy_amount as profit,
    cast(sell_amount-buy_amount as double)/cast(buy_amount as double)*100 as percentage,
    rank() over (order by profit desc) as rank
    from purchaser a join seller b on a.purchaser = b.seller
    where purchaser not in (select address
    from crosschain.core.address_labels)
    and purchaser not in (select address
    from solana.core.dim_labels)
    and b.date >= a.date
    and a.mint = b.mint
    group by 1
    having buy_tx = sell_tx)

    select count(DISTINCT(purchaser)) as user,
    case when buy_tx = 1 then 'a. 1 Trade'
    when buy_tx = 2 then 'b. 2 Trades'
    when buy_tx <= 10 then 'c. 2-10 Trades'
    when buy_tx <= 25 then 'd. 10-25 Trades'
    when buy_tx <= 50 then 'e. 25-50 Trades'
    when buy_tx <= 75 then 'f. 50-75 Trades'
    when buy_tx <= 100 then 'g. 75-100 Trades'
    when buy_tx > 100 then 'h. +100 Trades' end as cat
    from final
    where profit > 0 and buy_amount > 0 and sell_amount > 0
    group by 2