WITH holders AS (
SELECT
block_timestamp,
post_balance.value:owner AS wallet,
post_balance.value:mint AS token,
post_balance.value:uiTokenAmount:uiAmount AS amount,
ROW_NUMBER() OVER (PARTITION BY wallet, token ORDER BY block_timestamp DESC) AS rank
FROM solana.core.fact_transactions,
LATERAL FLATTEN(input => post_token_balances) post_balance
WHERE SUCCEEDED = 'True'
AND post_balance.value:owner = wallet
AND post_balance.value:mint = 'DUSTawucrTsGU8hcqRdHDCbuYhCPADMLM2VcCb8VnFnQ'
AND block_timestamp >= '2022-01-01'
ORDER BY 1,2,4
)
select
sum(amount)
FROM holders
WHERE rank = 1
AND amount IS NOT NULL
AND amount != ''