Updated 2024-11-27

    /*
    SELECT COUNT(DISTINCT tx_hash) AS "total swaps",
    COUNT(DISTINCT origin_from_address) AS "total users",
    SUM(amount_in_usd) AS "total swap volume",
    COUNT(DISTINCT tx_hash) *1.0 /COUNT(DISTINCT origin_from_address) AS "average tx per user",
    SUM(amount_in_usd) *1.0 /COUNT(DISTINCT origin_from_address) AS "average usd per user"
    FROM kaia.defi.ez_dex_swaps
    WHERE block_timestamp > CURRENT_TIMESTAMP - INTERVAL '30 days';
    */

    WITH total AS (
    SELECT
    SUM(amount_in_usd) AS "total swap volume",
    COUNT(DISTINCT tx_hash) AS "total swaps",
    COUNT(DISTINCT origin_from_address) AS "total users"
    FROM
    kaia.defi.ez_dex_swaps
    WHERE
    block_timestamp > CURRENT_TIMESTAMP - INTERVAL '30 days'
    ),

    averages AS (
    SELECT
    "total swap volume" / NULLIF("total users", 0) AS "average usd per user",
    "total swaps" * 1.0 / NULLIF("total users", 0) AS "average tx per user"
    FROM
    total
    ),

    daily AS (
    SELECT
    DATE(block_timestamp) AS "day",
    SUM(amount_in_usd) AS "daily volume",
    COUNT(DISTINCT tx_hash) AS "daily swaps",
    COUNT(DISTINCT origin_from_address) AS "daily users"
    QueryRunArchived: QueryRun has been archived