Query editor
with total as (with table1 as (with maxtxindex as (SELECT contract, "from", to, tx_hash as btx_hash, max(tx_log_index) as maxtx_log_index FROM (SELECT * FROM reservoir.sales rINNER JOIN looksrare_v2_ethereum.LooksRareProtocol_evt_AffiliatePayment l ON r.tx_hash = l.evt_tx_hash)GROUP BY 1,2,3,4),base as (SELECT * FROM maxtxindex mLEFT JOIN reservoir.sales rON m.contract = r.contract and m."from" = r."from" and m.to = r.to and m.btx_hash = r.tx_hash)SELECT affiliate, affiliateFee, cast(sum(r.price) as double) as rprice, p.price as usdprice, evt_tx_hash FROM base rINNER JOIN looksrare_v2_ethereum.LooksRareProtocol_evt_AffiliatePayment l ON r.btx_hash = l.evt_tx_hash and (r.maxtx_log_index + 1) = l.evt_indexLEFT JOIN prices.usd p ON date_trunc('minute', l.evt_block_time) = p.minuteWHERE blockchain = 'ethereum'AND symbol = 'WETH'AND affiliate NOT IN (0xc8bc7543aeb7fbf833fe3da2fa6c2e5443184d56, 0x34c4af1d4ce9e2223c5d8f2d743341c9d631f980, 0x9d7018db06706ace251d85e607c3a9cba717ebe5)AND aggregator_source IS NULLAND evt_block_time >= TIMESTAMP '2023-08-01'AND evt_block_time < TIMESTAMP '2023-09-01'GROUP BY 1,2,4,5UNION ALLSELECT DISTINCT affiliate, affiliateFee, cast(ETHTotal as double) as rprice, p.price as usdprice, evt_tx_hash FROM base rINNER JOIN looksrare_v2_ethereum.LooksRareProtocol_evt_AffiliatePayment l ON r.btx_hash = l.evt_tx_hashINNER JOIN query_2782534 q on r.btx_hash = q.tx_hashLEFT JOIN prices.usd p ON date_trunc('minute', l.evt_block_time) = p.minuteWHERE blockchain = 'ethereum'AND symbol = 'WETH'AND affiliate NOT IN (0xc8bc7543aeb7fbf833fe3da2fa6c2e5443184d56, 0x34c4af1d4ce9e2223c5d8f2d743341c9d631f980, 0x9d7018db06706ace251d85e607c3a9cba717ebe5)AND aggregator_source IS NOT NULLAND (100/(CAST(ETHTotal/1E18 as double)/(CAST(affiliateFee/1E18 as double)*100)))/0.5 IN (25.000, 50.000)AND evt_block_time >= TIMESTAMP '2023-07-01'AND evt_block_time < TIMESTAMP '2023-08-01')SELECT affiliate as Affiliate, 1/(CAST(rprice as double)/(CAST(affiliateFee as double)*100)) as "%EarningsOfOriginalSale", (100/(CAST(rprice as double)/(CAST(affiliateFee as double)*100)))/0.5 as "%ofProtocolFee",CAST(ROUND(sum(rprice/1E18), 3) AS DECIMAL(10,3)) as "Volume (ETH)",CONCAT(CAST(CAST(ROUND(sum(rprice*usdprice/1E18), 3) AS DECIMAL(10,3)) AS VARCHAR), ' $') as "Volume (USD)",CONCAT(CAST(CAST(ROUND(sum(affiliateFee/1E18), 3) AS DECIMAL(10,3)) AS VARCHAR), ' Ξ') as "Earnings (ETH)",CONCAT(CAST(CAST(ROUND(sum(affiliateFee*usdprice/1E18), 3) AS DECIMAL(10,3)) AS VARCHAR), ' $') as "Earnings (USD)",COUNT(DISTINCT evt_tx_hash) as TXCount FROM table1WHERE (100/(CAST(rprice/1E18 as double)/(CAST(affiliateFee/1E18 as double)*100)))/0.5 IN (25.000, 50.000)GROUP BY 1,2,3ORDER BY 6 DESC)SELECT sum("Volume (ETH)") FROM total