Table Schema
Inspect
Table
| Column | Type |
|---|---|
| license_id | integer |
| user_id | integer |
| assigned_date | date |
| status | varchar |
| Column | Type |
|---|---|
| user_id | integer |
| last_active_date | date |
Calculate the percentage of assigned licenses that are actively used.
Table Schema
| Column | Type |
|---|---|
| license_id | integer |
| user_id | integer |
| assigned_date | date |
| status | varchar |
| Column | Type |
|---|---|
| user_id | integer |
| last_active_date | date |
Sample Data
| license_id | user_id | assigned_date | status |
|---|---|---|---|
| 1 | 101 | 2024-01-01 | active |
| 2 | 102 | 2024-01-02 | active |
| 3 | 103 | 2024-01-03 | inactive |
| 4 | 104 | 2024-01-04 | active |
| 5 | 105 | 2024-01-05 | active |
| user_id | last_active_date |
|---|---|
| 101 | 2024-02-01 |
| 102 | 2024-02-10 |
| 104 | 2024-01-15 |
| utilization_rate |
|---|
| 75.00 |
SQL Editor
| utilization_rate |
|---|
| 75.00 |
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 ROUND(100.0 * COUNT(u.user_id) / COUNT(l.license_id), 2) AS utilization_rate FROM licenses l LEFT JOIN usage_logs u ON l.user_id = u.user_id WHERE l.status = 'active';
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