select minter, count(tx_id) as n_profitable_trade, avg(mint_price) as avg_init_capital
from (with m as (select program_id,mint,
tx_id, purchaser as minter,minT_price
from solana.fact_nft_mints
where mint_currency='So11111111111111111111111111111111111111111'
order by 1),
s as (
select mint,sales_amount, min(block_timestamp) as ft
from solana.fact_nft_sales
group by 1,2 order by 1
)
select minter, program_id,tx_id, mint_price, sales_amount, sales_amount-mint_price as profit, case
when profit>0 then 'profitted' else 'loss' end as pnl
from m inner join s on m.mint=s.mint)
where pnl='profitted'
group by 1 order by 2 desc
limit 100