Query editor
WITH tier_sales AS (SELECTt.tier,COALESCE(SUM(t.nodes_purchased), 0) AS sold, -- Total nodes soldCOALESCE(SUM(CASE WHEN t.round = 'WL' THEN t.nodes_purchased ELSE 0 END), 0) AS wl_sold, -- Nodes sold in WL roundCOALESCE(SUM(CASE WHEN t.round = 'Public' THEN t.nodes_purchased ELSE 0 END), 0) AS public_sold, -- Nodes sold in Public roundCOALESCE(SUM(t.USD_amount), 0) AS USD_amount, -- Total USD spent for the salesCOALESCE(SUM(CASE WHEN t.round = 'WL' THEN t.USD_amount ELSE 0 END), 0) AS wl_USD_amount, -- USD amount for WL salesCOALESCE(SUM(CASE WHEN t.round = 'Public' THEN t.USD_amount ELSE 0 END), 0) AS public_USD_amount -- USD amount for Public salesFROM query_4271957 AS tWHERE t.tier IS NOT NULLGROUP BY t.tier)SELECTts.tier,tp.tot_cnt AS max_nodes, -- Maximum number of nodes per tierts.sold, -- Total nodes sold-- Safe subtraction logic for nodes_leftCASEWHEN ts.sold > tp.tot_cnt THEN 0 -- Set to 0 if sold exceeds max_nodesELSE tp.tot_cnt - ts.sold -- Otherwise, calculate normallyEND AS nodes_left,tp.wl_cnt AS max_wl,ts.wl_sold,-- Adjust WL sold percentage to show 100% when oversoldCASEWHEN ts.wl_sold >= tp.wl_cnt THEN 1.0 -- Show 100% if WL sold >= max WLELSE ts.wl_sold * 1.0 / NULLIF(tp.wl_cnt, 0) -- Otherwise, calculate percentageEND AS wl_sold_pct, -- Percentage of WL nodes soldtp.public_cnt AS max_public,ts.public_sold,COALESCE(ts.public_sold * 1.0 / NULLIF(tp.public_cnt, 0), 0) AS public_sold_pct, -- Percentage of Public nodes soldCASEWHEN ts.sold >= tp.tot_cnt THEN 1.0 -- 100% if sold exceeds or equals max_nodesELSE ts.sold * 1.0 / NULLIF(tp.tot_cnt, 0) -- Otherwise, calculate percentageEND AS sold_pct, -- Percentage of nodes soldts.wl_USD_amount,ts.public_USD_amount,ts.USD_amountFROM tier_sales AS tsJOIN query_4272050 AS tp ON ts.tier = tp.tier -- Join with the tier data to get max countsORDER BY ts.tier ASC;