Query editor
WITHcombined_deposits AS (SELECT 'rysk' AS source, contract_address, recipient, DATE_TRUNC('day', evt_block_time) AS date_day, amount, evt_tx_hashFROM rysk_beyond_arbitrum.liquiditypool_evt_depositUNION ALLSELECT 'lyra' AS source, contract_address, beneficiary AS recipient, DATE_TRUNC('day', evt_block_time) AS date_day, amountDeposited AS amount, evt_tx_hashFROM lyra_newport_arbitrum.LiquidityPool_evt_DepositProcessed),combined_withdraws AS (SELECT 'rysk' AS source, contract_address, recipient, DATE_TRUNC('day', evt_block_time) AS date_day, amount, evt_tx_hashFROM rysk_beyond_arbitrum.liquiditypool_evt_withdrawUNION ALLSELECT 'lyra' AS source, contract_address, beneficiary AS recipient, DATE_TRUNC('day', evt_block_time) AS date_day, amountWithdrawn AS amount, evt_tx_hashFROM lyra_newport_arbitrum.LiquidityPool_evt_WithdrawProcessed),deposit_amounts AS (SELECTsource,CASEWHEN source = 'rysk' THEN SUM(amount) / POWER(10, 6)ELSE SUM(amount) / POWER(10, 18)END AS amount_depositsFROM combined_depositsGROUP BY source),withdraw_amounts AS (SELECTsource,CASEWHEN source = 'rysk' THEN SUM(amount) / POWER(10, 6)ELSE SUM(amount) / POWER(10, 18)END AS amount_withdrawsFROM combined_withdrawsGROUP BY source),source_aggregates AS (SELECT d.source,d.amount_deposits,w.amount_withdraws,d.amount_deposits - w.amount_withdraws AS net_amountFROM deposit_amounts dJOIN withdraw_amounts w ON d.source = w.source)-- Main data selection with the total net_amount as a new columnSELECT source,amount_deposits,amount_withdraws,net_amount,SUM(net_amount) OVER () AS total_net_amountFROM source_aggregates-- Including the Premia line (commented out)-- UNION ALL-- SELECT 'Premia', null, null, total_combined_balance_usd, SUM(net_amount) OVER ()-- FROM source_aggregates