Query editor
with trades as (select block_time, token_id, amount_usd, amount_original, currency_symbol, buyer, seller, tx_hashfrom nft.tradeswhere lower(cast(nft_contract_address as varchar)) = lower('{{nft_contract}}')UNIONselect block_time, token_id, amount_usd, amount_original, currency_symbol, buyer, seller, tx_hashfrom nft.mintswhere lower(cast(nft_contract_address as varchar)) = lower('{{nft_contract}}') and trade_category = 'Mint'),raw_data as (select block_time as sold_date,lag(block_time, 1) over (partition by t.token_id order by block_time asc) as previous_buy_day,amount_usd as sold_for_amount_usd,lag(amount_usd, 1, 0) over (partition by t.token_id order by block_time asc) as cost_basis_usd,amount_original as sold_for_amount_eth,lag(amount_original, 1, 0) over (partition by t.token_id order by block_time asc) as cost_basis,t.token_id, buyer, seller, currency_symbol, tx_hashfrom trades t)selectseller,token_id,previous_buy_day,sold_date,sold_for_amount_usd - cost_basis_usd as profit_usd,sold_for_amount_usd,cost_basis_usd,sold_for_amount_eth - cost_basis as profit_eth,sold_for_amount_eth,cost_basisfrom raw_dataorder by profit_usd desc