Query editor
WITH cex_addresses AS (SELECT DISTINCT from_utf8(a.address) AS addressFROM labels.owner_addresses aINNER JOIN labels.owner_details d ON a.owner_key = d.owner_keyWHERE a.blockchain = 'bitcoin'AND d.primary_category = 'cex'),inflows AS (SELECTt.block_date AS day,SUM(o.value) AS inflow_btcFROM bitcoin.transactions tCROSS JOIN UNNEST(t.output) AS o(index, script_pub_key, value)INNER JOIN cex_addresses c ON o.script_pub_key.address = c.addressWHERE t.block_date >= CURRENT_DATE - INTERVAL '30' dayAND t.block_date < CURRENT_DATEGROUP BY t.block_date),outflows AS (SELECTt.block_date AS day,SUM(i.value) AS outflow_btcFROM bitcoin.transactions tCROSS JOIN UNNEST(t.input) AS i(value, coinbase, height, tx_id, output_number, script_pub_key, script_signature, sequence, witness_data)INNER JOIN cex_addresses c ON i.script_pub_key.address = c.addressWHERE t.block_date >= CURRENT_DATE - INTERVAL '30' dayAND t.block_date < CURRENT_DATEGROUP BY t.block_date)SELECTCOALESCE(i.day, o.day) AS day,COALESCE(i.inflow_btc, 0) AS exchange_inflow_btc,COALESCE(o.outflow_btc, 0) AS exchange_outflow_btc,COALESCE(i.inflow_btc, 0) - COALESCE(o.outflow_btc, 0) AS net_exchange_flow_btcFROM inflows iFULL OUTER JOIN outflows o ON i.day = o.dayORDER BY day ASC