Query editor
WITH deposit_raw AS (SELECTid,-- Bid price in your desired unit (e.g. USD) using ETH price input(CAST(price AS double) / POWER(2, 96)) * (CAST({{ETH price}} AS double)) AS bid_price,SUM(CAST(amount AS double) / 1e18) AS deposited_ethFROM aztec_caa_sale_ethereum.continuousclearingauction_evt_bidsubmittedGROUP BY 1, 2),withdraw_raw AS (SELECTbidID AS id,SUM(CAST(currencyRefunded AS double) / 1e18) AS withdrawn_ethFROM aztec_caa_sale_ethereum.continuousclearingauction_evt_bidexitedGROUP BY bidID),net_bids AS (SELECTd.id,d.bid_price,GREATEST(d.deposited_eth - COALESCE(w.withdrawn_eth, 0),0) AS net_ethFROM deposit_raw dLEFT JOIN withdraw_raw wON d.id = w.id),filtered AS (SELECT *FROM net_bidsWHERE net_eth > 0 -- ignore fully withdrawn / negativeAND bid_price < (CAST({{Clearing Price}} AS double)) -- below clearing price),bucketed AS (SELECTCASEWHEN bid_price >= 0 AND bid_price < 0.01 THEN '0 - 0.01'WHEN bid_price >= 0.01 AND bid_price < 0.02 THEN '0.01 - 0.02'WHEN bid_price >= 0.02 AND bid_price < 0.03 THEN '0.02 - 0.03'WHEN bid_price >= 0.03 AND bid_price < 0.04 THEN '0.03 - 0.04'WHEN bid_price >= 0.04 AND bid_price < 0.05 THEN '0.04 - 0.05'WHEN bid_price >= 0.05 AND bid_price < 0.06 THEN '0.05 - 0.06'ELSE 'outside_range'END AS price_bucket,net_ethFROM filtered)-- main grouped rowsSELECTprice_bucket,SUM(net_eth) AS total_net_eth,COUNT(*) AS bid_countFROM bucketedGROUP BY price_bucket