Query editor
WITH prices_primary AS (SELECTDATE_TRUNC('day', timestamp) AS day,AVG(price_usd * 1e9) AS price_usdFROM ton.prices_dailyWHERE token_address = '0:0000000000000000000000000000000000000000000000000000000000000000'GROUP BY 1),prices_fallback AS (SELECTday,AVG(price) AS price_usdFROM prices.usd_dailyWHERE symbol = 'TON' AND blockchain IS NULLGROUP BY 1),prices_combined AS (SELECT COALESCE(p1.day, p2.day) AS day, COALESCE(p1.price_usd, p2.price_usd) AS price_usdFROM prices_primary p1FULL OUTER JOIN prices_fallback p2 ON p1.day = p2.day),daily_txs_and_fees AS (SELECTDATE(block_time) AS day,COUNT(DISTINCT hash) AS daily_transactions,SUM(total_fees) / 1e9 AS daily_fees_tonFROM ton.transactionsWHERE block_time IS NOT NULLGROUP BY 1),date_series AS (SELECT CAST(exploded_day AS DATE) AS dayFROM UNNEST(sequence(CAST((SELECT MIN(day) FROM daily_txs_and_fees) AS DATE),CURRENT_DATE,INTERVAL '1' day)) AS t(exploded_day))SELECTds.day AS "Date",COALESCE(dtf.daily_transactions, 0) AS "Daily Transactions",SUM(COALESCE(dtf.daily_transactions, 0)) OVER (ORDER BY ds.day) AS "Cumulative Transactions",ROUND(COALESCE(dtf.daily_fees_ton * p.price_usd, 0), 2) AS "Daily Gas Fees (USD)",SUM(ROUND(COALESCE(dtf.daily_fees_ton * p.price_usd, 0), 2)) OVER (ORDER BY ds.day) AS "Cumulative Gas Fees (USD)",COALESCE(dtf.daily_fees_ton / NULLIF(dtf.daily_transactions, 0), 0) AS "Avg Gas Fee (TON)",ROUND(COALESCE((dtf.daily_fees_ton / NULLIF(dtf.daily_transactions, 0)) * p.price_usd, 0), 4) AS "Avg Gas Fee (USD)"FROM date_series dsLEFT JOIN daily_txs_and_fees dtf ON ds.day = dtf.dayLEFT JOIN prices_combined p ON ds.day = p.dayWHERE ds.day < CURRENT_DATE - INTERVAL '1' DAYORDER BY ds.day DESC