Table Schema
Inspect
Table
| Column | Type |
|---|---|
| user_id | integer |
| product_name | varchar |
| usage_date | date |
Find monthly active users for each product.
Table Schema
| Column | Type |
|---|---|
| user_id | integer |
| product_name | varchar |
| usage_date | date |
Sample Data
| user_id | product_name | usage_date |
|---|---|---|
| 101 | Teams | 2024-01-01 |
| 102 | Teams | 2024-01-02 |
| 103 | Outlook | 2024-01-03 |
| 101 | Teams | 2024-02-01 |
| 104 | Outlook | 2024-02-02 |
| 105 | Teams | 2024-02-03 |
| month | product_name | active_users |
|---|---|---|
| 2024-01 | Outlook | 1 |
| 2024-01 | Teams | 2 |
| 2024-02 | Outlook | 1 |
| 2024-02 | Teams | 2 |
SQL Editor
| month | product_name | active_users |
|---|---|---|
| 2024-01 | Outlook | 1 |
| 2024-01 | Teams | 2 |
| 2024-02 | Outlook | 1 |
| 2024-02 | Teams | 2 |
Hints
Solution
Solution is locked until you decide to reveal it. Try the editor first, then open this when you want the reference answer.
SELECT TO_CHAR(DATE_TRUNC('month', usage_date), 'YYYY-MM') AS month, product_name, COUNT(DISTINCT user_id) AS active_users
FROM product_usage
GROUP BY DATE_TRUNC('month', usage_date), product_name;Explanation
Read the expected output columns to determine the final grain.
Aggregate or rank the input rows to calculate the requested metric.
Filter, sort, and alias the final columns to match the output.
Related Questions