Query editor
with polygon_treasury as (SELECT time, contract_address, SUM(amount) as total_amountFROM (SELECT date_trunc('day', evt_block_time) as time, contract_address, CAST(value as DECIMAL(38,0)) as amount-- , 'DAI' as tokenFROM erc20_polygon.evt_TransferWHERE-- contract_address = 0x6b175474e89094c44da98b954eedeac495271d0f-- and"to" = 0xdca05161ee5b5fa6df170191c88857e70ffb4094UNION ALLSELECT date_trunc('day', evt_block_time) as time, contract_address, -CAST(value as DECIMAL(38,0)) as amount-- , 'DAI' as tokenFROM erc20_polygon.evt_TransferWHERE-- contract_address = 0x6b175474e89094c44da98b954eedeac495271d0f-- and"from" = 0xdca05161ee5b5fa6df170191c88857e70ffb4094)GROUP BY 1, 2)SELECT time, IF(weth_cumulative_value = 0, NULL, weth_cumulative_value) as weth_cumulative_value, IF(dai_cumulative_value = 0, NULL, dai_cumulative_value) AS dai_cumulative_value, IF(usdt_cumulative_value = 0, NULL, usdt_cumulative_value) AS usdt_cumulative_value, IF(usdc_cumulative_value = 0, NULL, usdc_cumulative_value) AS usdc_cumulative_value, IF(wbtc_cumulative_value = 0, NULL, wbtc_cumulative_value) AS wbtc_cumulative_value, IF(matic_cumulative_value = 0, NULL, matic_cumulative_value) AS wmatic_cumulative_value, IF(other_cumulative_value = 0, NULL, other_cumulative_value) AS other_cumulative_value, IF(total_cumulative_value = 0, NULL, total_cumulative_value) AS total_cumulative_value, IF(total_cumulative_value = 0, NULL, total_cumulative_value)/1e3 AS total_cumulative_value_formatFROM (SELECT date_series.time,SUM(IF(symbol = 'WETH', token_value, 0)) over (order by date_series.time) as weth_cumulative_value,SUM(IF(symbol = 'DAI', token_value, 0)) over (order by date_series.time) as dai_cumulative_value,SUM(IF(symbol = 'USDC', token_value, 0)) over (order by date_series.time) as usdc_cumulative_value,SUM(IF(symbol = 'USDT', token_value, 0)) over (order by date_series.time) as usdt_cumulative_value,SUM(IF(symbol = 'WBTC', token_value, 0)) over (order by date_series.time) as wbtc_cumulative_value,SUM(IF(symbol = 'WMATIC', token_value, 0)) over (order by date_series.time) as matic_cumulative_value, SUM(IF(symbol NOT in ('WETH', 'DAI', 'USDC', 'USDT', 'WBTC', 'WMATIC'), token_value, 0)) over (order by date_series.time) as other_cumulative_value, SUM(token_value) over (order by date_series.time) as total_cumulative_valueFROM (SELECT polygon_treasury.time ,tokens.symbol,pricing.avg_price * sum( total_amount/POW(10, tokens.decimals)) as token_valueFROM polygon_treasuryJOIN (SELECT 'polygon' as blockchain, 0x92a9c92c215092720c731c96d4ff508c831a714f as contract_address,18 as decimals, 'RAILPOLY' as symbolUNION ALLSELECT blockchain, contract_address, decimals, symbolFROM tokens.erc20WHERE blockchain = 'polygon') AS tokens on polygon_treasury.contract_address = tokens.contract_addressJOIN (SELECT date_trunc('day', hour) as time, contract_address, AVG(median_price) as avg_price