Query editor
WITH weighted AS (SELECT *,date_diff('second',greatest(snapshot_utc-INTERVAL '1' HOUR,CAST(created AS timestamp),(CAST(date_trunc('hour',current_timestamp) AS timestamp)-INTERVAL '26' HOUR)),snapshot_utc)/3600.0 weight_hours FROM query_8666199 WHERE snapshot_utc>(CAST(date_trunc('hour',current_timestamp) AS timestamp)-INTERVAL '26' HOUR) AND snapshot_utc<=(CAST(date_trunc('hour',current_timestamp) AS timestamp)-INTERVAL '2' HOUR)), capital AS (SELECT pool_id,role,pool,min(created) created,sum(principal_shroom_equiv*weight_hours)/nullif(sum(weight_hours),0) average_capital_shroom_equiv,max_by(principal_shroom_equiv,snapshot_utc) closing_capital_shroom_equiv,sum(negative_ranges) negative_ranges,count(*) observed_hour_ends,count_if(principal_shroom_equiv>0) funded_hour_ends,sum(weight_hours) observation_hours FROM weighted GROUP BY 1,2,3), flow AS (SELECT pool_id,sum(swaps) swaps,sum(shroom_leg_volume) shroom_leg_volume,sum(gross_input_shroom_equiv) gross_input_shroom_equiv,sum(lp_fees_shroom_equiv) lp_fees_shroom_equiv,sum(protocol_fees_shroom_equiv) protocol_fees_shroom_equiv,sum(lp_fees_token0) lp_fees_token0,sum(lp_fees_shroom) lp_fees_shroom,sum(fee_formula_mismatches) fee_formula_mismatches FROM query_8666195 WHERE hour_utc>=(CAST(date_trunc('hour',current_timestamp) AS timestamp)-INTERVAL '26' HOUR) AND hour_utc<(CAST(date_trunc('hour',current_timestamp) AS timestamp)-INTERVAL '2' HOUR) GROUP BY 1) SELECT (CAST(date_trunc('hour',current_timestamp) AS timestamp)-INTERVAL '2' HOUR) observation_end_utc,current_timestamp refreshed_utc,c.role,c.pool,c.pool_id,c.created,c.observation_hours age_in_window_hours,c.observed_hour_ends,c.funded_hour_ends,c.average_capital_shroom_equiv,c.closing_capital_shroom_equiv,coalesce(f.swaps,0) swaps,coalesce(f.shroom_leg_volume,0) shroom_leg_volume,coalesce(f.lp_fees_shroom_equiv,0) lp_fees_shroom_equiv,coalesce(f.protocol_fees_shroom_equiv,0) protocol_fees_shroom_equiv,coalesce(f.lp_fees_token0,0) lp_fees_token0,coalesce(f.lp_fees_shroom,0) lp_fees_shroom,coalesce(f.lp_fees_shroom_equiv,0)/nullif(c.average_capital_shroom_equiv,0) capital_yield_window,coalesce(f.shroom_leg_volume,0)/nullif(c.average_capital_shroom_equiv,0) capital_velocity_window,c.negative_ranges,coalesce(f.fee_formula_mismatches,0) fee_formula_mismatches FROM capital c LEFT JOIN flow f ON c.pool_id=f.pool_id ORDER BY c.role,c.pool