0xHaM-dRetention
    Updated 2025-04-30
    -- forked from Retention @ https://flipsidecrypto.xyz/edit/queries/c0cb060e-fca1-4c21-9399-e460b535d396

    -- forked from Retention by Dune @ https://flipsidecrypto.xyz/edit/queries/821afa83-97c1-49de-bc53-f515ada55660

    WITH
    users AS (
    select
    block_timestamp,
    tx_hash,
    TX_SIGNER
    FROM near.core.fact_transactions
    WHERE block_timestamp::date > '2023-01-01'
    ),
    firstUserOccurrences AS (
    SELECT
    TX_SIGNER,
    MIN(users.BLOCK_TIMESTAMP::date) AS firstTradeDate
    FROM
    users
    GROUP BY
    TX_SIGNER
    ),
    statsByDay AS (
    SELECT
    date_trunc('week',BLOCK_TIMESTAMP)::date as date,
    COUNT(DISTINCT (users.TX_SIGNER)) AS num_users,
    COALESCE(COUNT(DISTINCT (firstUserOccurrences.TX_SIGNER)), 0) AS num_New_users,
    COUNT(DISTINCT (users.TX_SIGNER)) - COALESCE(COUNT(DISTINCT (firstUserOccurrences.TX_SIGNER)), 0) AS num_Returning_users,
    SUM(
    COALESCE(COUNT(DISTINCT (firstUserOccurrences.TX_SIGNER)), 0)
    ) OVER (
    ORDER BY
    date
    ) AS cum_New_users
    FROM
    users
    QueryRunArchived: QueryRun has been archived