Query editor
WITH transfers AS (SELECT -- OutflowsDATE_TRUNC('day', evt_block_time) AS "date", tr."from" AS address, -CAST(tr.value AS DOUBLE) AS amount, contract_addressFROM erc20_{{network}}.evt_Transfer trWHERE contract_address = {{token_address}}UNION ALLSELECT -- InflowsDATE_TRUNC('day', evt_block_time) AS "date", tr."to" AS address, CAST(tr.value AS DOUBLE) AS amount, contract_addressFROM erc20_{{network}}.evt_Transfer trWHERE contract_address = {{token_address}}), daily_balances_with_gaps AS (SELECTt."date", address, SUM(amount) OVER (PARTITION BY address ORDER BY t."date") AS balance -- Balance for each day with a transfer (Balance isn't decimal adjusted), LEAD("date", 1, CURRENT_DATE) OVER (PARTITION BY address ORDER BY t."date") AS next_day -- The day after a day with a transfer-- This is the field that then creates the forward fill effect and the increased granularityFROM transfers t), date_seq AS (SELECT SEQUENCE(CAST('2016-01-20' AS DATE), DATE_TRUNC('day', CURRENT_DATE)) AS "date" -- Generate all days since the first contract), date_series AS (SELECT days."date"FROMdate_seq CROSS JOIN UNNEST("date") AS days("date") -- This is just doing explode like in spark sql), daily_balances_all AS (SELECTd."date", b.address AS address, b.balance AS balanceFROMdaily_balances_with_gaps b INNER JOIN date_series dON b."date" <= d."date"AND d."date" < b.next_day-- Yields an observation for every day after the first transfer until the next day with transfer)SELECT "date", COUNT(address) AS token_holdersFROM daily_balances_allWHERE balance > 0 -- POWER(10 ,18) -- Review the divisorGROUP BY "date"