Query editor
-- Creating a base query for the trades with necessary fields extracted and cleanedWITH trades AS (SELECTregexp_extract(trim( '\u00' from tracking_code), '([A-z]+)', 1) as tracking_code,regexp_extract(trim( '\u00' from marketKey), '([A-z]+)', 1) as marketKey,regexp_extract(trim( '\u00' from PERP), '([A-z]+)', 1) as PERP,contract_address,evt_tx_hash,evt_block_time,account,fee,lastPrice,margin,size,tradeSize,volume,totalFee,helper,day,date_trunc('month', evt_block_time) as month -- Using month instead of day for aggregationFROM query_2390745WHERE evt_block_time > now() - interval '90' day AND PERP IS NOT NULL),-- Calculate total fees per monthmonthly_totals AS (SELECTmonth,sum(fee) as total_feesFROM tradesGROUP BY month),-- Calculate fees per tracking_code per monthfees_per_code AS (SELECTmonth,coalesce(tracking_code, 'OTHER') as tracking_code,sum(fee) as feesFROM tradesGROUP BY month, tracking_code)-- Calculate the percentage of fees per tracking_code per month, and scale it by 10,000-- and include contract address based on tracking_code mapping, made case-insensitiveSELECTfpc.month,fpc.tracking_code,fpc.fees,mt.total_fees,(fpc.fees / mt.total_fees * 100.0) AS percentage_of_fees,(fpc.fees / mt.total_fees * 10000.0) AS scaled_percentage_of_fees, -- Scaled by 10,000CASE LOWER(fpc.tracking_code)WHEN 'dhedge' THEN '0x352Fb838A3ae9b0ef2f0EBF24191AcAf4aB9EcEc'WHEN 'kwenta' THEN '0x82d2242257115351899894eF384f779b5ba8c695'WHEN 'polynomial' THEN '0x7cb6bF3e7395965b2162A7C2e6876720C20012d6'WHEN 'decentrex' THEN '0x8c5502973FeeD780795AF2Fa54FbfF736065c3A7'WHEN 'conduit' THEN '0x463A522F657e1c591c495d688c8627bA2d9342dE'WHEN 'valio' THEN '0xA643246226c3A2d1c0B3987Ea6dA93C37FdACaBa'WHEN 'copin' THEN '0x1A561e43f264952D7b4F25Fb4967122c87618Beb'