0xHaM-dKaia Swap User- Time based Cohort Wide
    Updated 2024-10-17
    -- forked from TFM Swapper Time based Cohort Wide @ https://flipsidecrypto.xyz/edit/queries/98de3481-59c3-469a-9c05-1d99ee0a8d78

    with qmain as (
    SELECT
    block_timestamp,
    tx_hash,
    from_address as user,
    FROM kaia.core.fact_transactions tx
    JOIN (
    SELECT
    distinct tx_hash,
    block_timestamp
    FROM kaia.core.fact_event_logs
    join kaia.core.dim_labels on contract_address = address
    where label_type = 'dex'
    AND tx_succeeded = 'TRUE'
    ) using(block_timestamp, tx_hash)
    )

    , base_table as ( -- this is data prep
    select
    user as address
    , date_trunc('month', block_timestamp) as date
    , min(date_trunc('month', block_timestamp)) over(partition by address) as earliest_date
    , datediff(
    'month'
    , min(date_trunc('month', block_timestamp)) over(partition by address) -- earliest_date
    , date_trunc('month', block_timestamp) -- current date in month
    ) as difference
    from qmain
    where block_timestamp >= current_timestamp() - interval '13 month'
    HAVING date_trunc('month', block_timestamp) < date_trunc('month', current_date())
    )

    , count_new_users as(
    select