Query editor
WITH params AS (SELECT0.01 AS hf_bin_size,1.5 AS hf_max,TIMESTAMP '2024-01-01' AS start_time),aave_reserves AS (SELECT DISTINCTasset AS reserveFROM aave_v3_ethereum.poolconfigurator_evt_reserveinitialized),pt_reserves AS (SELECTr.reserve,COALESCE(t.symbol, CAST(r.reserve AS varchar)) AS pt_symbol,COALESCE(t.name, CAST(r.reserve AS varchar)) AS pt_name,COALESCE(t.decimals, 18) AS decimalsFROM aave_reserves rLEFT JOIN tokens.erc20 tON t.blockchain = 'ethereum'AND t.contract_address = r.reserveWHERElower(t.symbol) LIKE 'pt-%'OR lower(t.name) LIKE '%principal token%'OR lower(t.name) LIKE '%pendle%'),pt_user_flows AS (SELECTs.reserve,s.onBehalfOf AS user,CAST(s.amount AS double) AS amount_rawFROM aave_v3_ethereum.pool_evt_supply sJOIN pt_reserves pON s.reserve = p.reserveUNION ALLSELECTw.reserve,w.user,-CAST(w.amount AS double) AS amount_rawFROM aave_v3_ethereum.pool_evt_withdraw wJOIN pt_reserves pON w.reserve = p.reserve),pt_users_by_market AS (SELECTf.reserve,p.pt_symbol,p.pt_name,f.user,SUM(f.amount_raw / POWER(10, p.decimals)) AS net_pt_amountFROM pt_user_flows fJOIN pt_reserves pON f.reserve = p.reserveGROUP BY 1, 2, 3, 4