Have you ever struggled with calculating your active users per day, or month? How about your new activations or churned users? It's a common problem, but often the first solution you find will not scale. You should avoid sub queries, and use a window function instead. But that's only a part of the solution. Assuming your active user definition is "someone who has signed in within 7 days" and you have this data in a table with columns user_id , timestamp . The first thing you want to do is calculate when a user activates for the first time, when they churn and when they resurrect. CREATE OR REPLACE VIEW "vw_login" AS SELECT * , LEAST (LEAD("timestamp") OVER w, "timestamp" + 7) AS "activeExpiry" , CASE WHEN LAG("timestamp") OVER w IS NULL THEN true ELSE false AS "activated" , CASE WHEN LEAD("timestamp") OVER w IS NULL THEN true WHEN LEAD("timestamp") OVER w - "t