Query editor
-- This query used incompatible data types from Dune SQL alpha and may need to be updated.-- More details can be found on https://dune.com/docs/query/dunesql-changes/withbase as (SELECTdistinctdate_trunc('month', tx.block_time) as month, tx."from" as userFROM {{chain}}.traces trLEFT JOIN {{chain}}.transactions tx ON tx.hash = tr.tx_hash-- LEFT JOIN labels.contracts c ON c.blockchain = '{{chain}}' AND c.address = {{contract}}-- LEFT JOIN decoding.evm_signatures sig_fx ON substring(tr.input,1,10) = sig_fx.id-- AND sig_fx.type = 'function_call'-- AND lower(c.name) = lower(concat(sig_fx.namespace, ': ', sig_fx.name))WHERE tr.to = {{contract}}AND tr.successAND tr.block_time >= now() - interval '{{months ago}}' month-- AND ('{{modify_only}}' = 'false' OR (json_value(sig_fx.abi,'strict $.stateMutability') NOT IN ('pure', 'view') OR sig_fx.abi is null))), user_cohort as (--get the cohort start month for a specific user addressSELECTmin(month) as cohort, userFROM basegroup by 2), cohort_size as (SELECTcohort, count(*) as total_sizeFROM user_cohortgroup by 1), active_by_cohort as (SELECTcohort, month, row_number() OVER (partition by cohort order by month asc) as months, active_addressesFROM (SELECTu.cohort, b.month, count(*) as active_addressesFROM base bLEFT JOIN user_cohort u ON u.user = b.usergroup by 1,2)), retention_base as (SELECTa.cohort, total_size, map_agg(months, cast(active_addresses as double)/cast(total_size as double)) as retention_keyFROM active_by_cohort a