Write a SQL query using window functions to find the top three most active workspaces for each organization over the last 30 days.
Use the organizations, workspaces, and workspace_activity tables. Activity is measured by the number of activity records. Include only workspaces with activity during the period.
Output
- One row per qualifying workspace, with
organization_id, organization_name, workspace_id, workspace_name, activity_count, and activity_rank.
- Return at most three workspaces per organization, ordered by organization, rank, and workspace ID. Break equal activity counts by ascending
workspace_id.
Output details
- Treat
DATE '2026-09-15' as today's date.
- Include activities with
activity_at on or after DATE '2026-08-16' and before DATE '2026-09-16'.
- Order by
organization_id ascending, then activity_rank ascending, then workspace_id ascending.
- Preserve
NULL organization_name values in the output.