1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

  2. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

User Life Cycle SQL Query Logic in Snowflake

Discussão em 'Outras Linguagens' iniciado por Stack, Maio 19, 2021.

  1. Stack

    Stack Membro Participativo

    I am working on building a query to track the life cycle of an user through the platform via events. The table EVENTS has 3 columns USER_ID, DATE_TIME and EVENT_NAME. Below is a snapshot of the table,

    [​IMG]

    My query should return the below result (the first timestamp for the registered event followed by the immediate/next timestamp of the following log_in event and finally followed by the immediate/next timestamp of the final landing_page event),

    [​IMG]

    Below is my query ,

    WITH FIRST_STEP AS
    (SELECT
    USER_ID,
    MIN(CASE WHEN EVENT_NAME = 'registered' THEN DATE_TIME ELSE NULL END) AS REGISTERED_TIMESTAMP
    FROM EVENTS
    GROUP BY 1
    ),
    SECOND_STEP AS
    (SELECT * FROM EVENTS
    WHERE EVENT_NAME = 'log_in'
    ORDER BY DATE_TIME
    ),
    THIRD_STEP AS
    (SELECT * FROM EVENTS
    WHERE EVENT_NAME = 'landing_page'
    ORDER BY DATE_TIME
    )
    SELECT
    a.USER_ID,
    a.REGISTERED_TIMESTAMP,
    (SELECT
    CASE WHEN b.DATE_TIME >= a.REGISTRATIONS_TIMESTAMP THEN b.DATE_TIME END AS LOG_IN_TIMESTAMP
    FROM SECOND_STEP
    LIMIT 1
    ),
    (SELECT
    CASE WHEN c.DATE_TIME >= LOG_IN_TIMESTAMP THEN c.DATE_TIME END AS LANDING_PAGE_TIMESTAMP
    FROM THIRD_STEP
    LIMIT 1
    )
    FROM FIRST_STEP AS a
    LEFT JOIN SECOND_STEP AS b ON a.USER_ID = b.USER_ID
    LEFT JOIN THIRD_STEP AS c ON b.USER_ID = c.USER_ID;


    Unfortunately I am getting the "SQL compilation error: Unsupported subquery type cannot be evaluated" error when I try to run the query

    Continue reading...

Compartilhe esta Página