-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeptStats.sql
More file actions
32 lines (31 loc) · 1.15 KB
/
DeptStats.sql
File metadata and controls
32 lines (31 loc) · 1.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
-- Group by department and obtain average salary and standard deviation for each department
WITH DeptStats AS (
SELECT Department,
STDDEV(Salary) AS Standard_Deviation,
AVG(Salary) AS Average,
COUNT(*) AS Department_Count
FROM Employee_Salaries
WHERE Salary >= 10000
GROUP BY Department
),
-- Identify outliers
DeptOutliers AS (
SELECT emp.Department,
emp.Salary,
dt.Standard_Deviation,
dt.Average,
(emp.Salary - dt.Average) / dt.Standard_Deviation as ZScore
FROM Employee_Salaries emp
JOIN DeptStats dt ON emp.Department = dt.Department
WHERE emp.Salary >= 10000
)
SELECT dt.Department,
ROUND(dt.Average,2) AS Average_Salary,
ROUND(dt.Standard_Deviation,2) AS Standard_Deviation,
ROUND((dt.Standard_Deviation / dt.Average),2) AS Coefficient_of_Variation,
dt.Department_Count,
SUM(CASE WHEN (do.zscore > 1.96 OR do.ZScore < -1.96) THEN 1 ELSE 0 END) AS Outlier_Count
FROM DeptStats dt
LEFT JOIN DeptOutliers do ON dt.Department = do.Department
GROUP BY dt.Department, dt.Standard_Deviation, dt.Average, dt.Standard_Deviation / dt.Average
ORDER BY Outlier_Count DESC;