Write a PostgreSQL query to identify customers whose websites have excessively high average resource usage.
Table schemas:
- The customers table contains id (INT, primary key) and email (VARCHAR(255)).
- The site_metrics table contains customer_id (INT, foreign key), cpu_usage (DECIMAL(5,2)), memory_usage (DECIMAL(5,2)), and disk_usage (DECIMAL(5,2)).
Specific requirements: - The report must show customers whose average usage exceeds 50% for any resource: CPU, memory, or disk.
- The result must include email, average_cpu_usage (average CPU usage, rounded to 2 decimal places), average_memory_usage (average memory usage, rounded to 2 decimal places), and average_disk_usage (average disk usage, rounded to 2 decimal places).
- Sort the results by email in ascending order (ASC).
- Filtering logic: First calculate each customer's average usage across all websites, then select customers for whom average_cpu_usage > 50 or average_memory_usage > 50 or average_disk_usage > 50. That means using HAVING to filter the aggregated results, rather than using WHERE during the JOIN to filter individual records.
Discussion
Loading comments…