Query editor
with gcoin_txt as (SELECTa.call_block_time as block_date,a.call_tx_hash,b.n AS gcoin_path,b.path_token as gcoin_contract,cast(b.output_amounts_token as double)/1e18 as gcoin_amtFROM sushiswap_polygon.UniswapV2Router02_call_swapExactTokensForTokens aCROSS JOIN UNNEST(a.path,a.output_amounts) WITH ORDINALITY AS b (path_token,output_amounts_token,n)where path_token=0x071ac29d569a47ebffb9e57517f855cb577dcc4c and call_success=trueand 1 <= {{Synchronize}}),weth_txt as (SELECTa.call_tx_hash,b.n AS weth_path,b.path_token as weth_contract,cast(b.output_amounts_token as double)/1e18 as weth_amtFROM sushiswap_polygon.UniswapV2Router02_call_swapExactTokensForTokens aCROSS JOIN UNNEST(a.path,a.output_amounts) WITH ORDINALITY AS b (path_token,output_amounts_token,n)where path_token=0x7ceb23fd6bc0add59e62ac25578270cff1b9f619 and call_success=true),price_info as (select date_trunc('day', block_date) as block_date, sum(weth_amt)/sum(gcoin_amt) as weth_per_gcoin, sum(gcoin_amt) as gcoin_volume from(--select date_trunc('minute', block_date) as block_time, sum(weth_amt) as weth_amt,sum(gcoin_amt) as gcoin_amt, price from(select * from gcoin_txt g left join weth_txt w on g.call_tx_hash = w.call_tx_hash) ft--left join prices.usd pl on ft.block_time = pl.minute and pl.blockchain="polygon"group by 1order by block_date)select *,Lag(gcoin_price, 7) OVER(order by block_date asc) AS price_7days_lag,(gcoin_price/(Lag(gcoin_price, 7) OVER(order by block_date asc))-1)*100 AS price_7days_changefrom (select pl.block_date, weth_per_gcoin, wp.weth_price, weth_per_gcoin* wp.weth_price as gcoin_price,gcoin_volume*weth_per_gcoin* wp.weth_price as trade_volume from price_info plleft join(select date_trunc('day', minute) as block_date, approx_percentile(price,0.5) as weth_price from prices.usdwhere symbol='WETH' and blockchain='polygon' and minute> cast('2022-01-17 00:00' as timestamp)group by 1) wpon pl.block_date = wp.block_date)where block_date > now() - interval '{{Trailing Months}}' month-------------alternative method if prices.usd is broken--------select *,-- Lag(gcoin_price, 7) OVER(order by block_date asc) AS price_7days_lag,-- (gcoin_price/(Lag(gcoin_price, 7) OVER(order by block_date asc))-1)*100 AS price_7days_change--from (--select pl.block_date, volume, weth_per_gcoin, weth_price, weth_per_gcoin * weth_price as gcoin_price from price_info pl--left join-- (select date_trunc('day', hour) as block_date,