0

Can you please help me with how I can adjust the query that I can run in SQL Server instead of Postgres?

WITH j AS 
(
    SELECT
        date_trunc( 'month', occurred_at::date ) AS month,
        array_agg( distinct user_id ) AS users,
        count( distinct user_id ) AS total_users
    FROM
        yammer_events
    GROUP BY
        1
    ORDER BY
        1
)
SELECT
    month::date,

    cardinality(LAG(users) OVER w - users) AS churn_count,
    (
        cardinality( LAG(users) OVER w - users )::numeric
        / 
        LAG(total_users) OVER w::numeric
    ) * 100 AS churn_rate_percentage

FROM
    j
WINDOW
    w AS (ORDER BY month ROWS BETWEEN 1 PRECEDING AND CURRENT ROW );

Converting Postgres query to SQL Server query

0 Answers0