-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSQL Analysis- Testing Tableau & Power BI Reports
185 lines (157 loc) · 5.69 KB
/
SQL Analysis- Testing Tableau & Power BI Reports
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
-- TESTING TABLEAU/ POWER BI REPORTS IN SQL
--KPI:
--1) Employee Count-
--Data Validation
select sum(employee_count) as Employee_Count from hrdata;
--Functional Validation:
select sum(employee_count) as Employee_Count from hrdata;
select sum(employee_count) as Employee_Count from hrdata
where education ='High School';
select sum(employee_count) as Employee_Count from hrdata
where department = 'R&D';
select sum(employee_count) as Employee_Count from hrdata
where education_field = 'Medical';
--2) Attrition Count-
--Data Validation:
select count(attrition) from hrdata
where attrition = 'Yes'
--Functional Validation:
select count(attrition) from hrdata
where attrition = 'Yes' AND department = 'R&D' and education_field= 'Medical'
and education= 'High School';
--3) Attrition Rate-
--Data Validation:
select ROUND(CAST((select count(attrition) from hrdata where attrition='Yes' ) AS float) /
NULLIF(sum(employee_count), 0) * 100, 2) AS Attrition_count from hrdata
--Functional Validation:
select ROUND(CAST((select count(attrition) from hrdata where attrition='Yes' and department='Sales') AS DECIMAL) /
NULLIF(sum(employee_count), 0) * 100, 2) AS Attrition_count from hrdata
where department='Sales'
--4) Active Employee-
--Data Validation:
select sum(employee_count) - (select count(attrition) from hrdata where attrition='Yes') as Active_Employee from hrdata
--Functional Validation:
select sum(employee_count) - (select count(attrition) from hrdata where attrition='Yes' and gender='Male') as Active_Employee from hrdata
where gender='Male'
--5) Average Age-
--Data Validation:
select round(sum(age)/count(emp_no),2) from hrdata
--Functional Validation:
select round(sum(age)/count(emp_no),2) from hrdata
where gender='Female'
--CHARTS:
--6) Attrition by Gender-
--Data Validation:
select gender,count(attrition) from hrdata
where attrition='Yes'
group by gender
order by count(attrition) desc
--Functional Validation:
select gender,count(attrition) from hrdata
where attrition='Yes' and education='High School'
group by gender
order by count(attrition) desc
7) Department wise Attrition-
--Data Validation:
select department, count(attrition),
Round((cast(count(attrition) as float) /
(select cast(count(attrition) as float) from hrdata where attrition='Yes'))*100, 2)
from hrdata
where attrition='Yes'
group by department
order by count(attrition) desc
--Functional Validation:
select department, count(attrition),
Round((cast(count(attrition) as float) /(select cast(count(attrition) as float) from hrdata where attrition='Yes'))*100, 2)
from hrdata
where attrition='Yes' and gender='Male'
group by department
order by count(attrition) desc
8) No of Employee by Age Group-
--Data Validation:
select age,sum(employee_count) from hrdata
group by age
order by age
--Functional Validation:
select age,sum(employee_count) from hrdata
where gender='Female'
group by age
order by age
--9) Education Field wise Attrition-
--Data Validation:
select education_field,count(attrition) from hrdata
where attrition='Yes'
group by education_field
order by count(attrition) desc
--Functional Validation:
select education_field,count(attrition) from hrdata
where attrition='Yes' and department='Sales'
group by education_field
order by count(attrition) desc
--10) Attrition Rate by Gender for different Age Group-
--Data Validation:
select age_band, gender, count(attrition) as attrition,
round((cast(count(attrition) as numeric) / (select count(attrition) from hrdata where attrition = 'Yes')) * 100,2) as pct
from hrdata
where attrition = 'Yes'
group by age_band, gender
order by age_band, gender desc;
--Functional Validation:
select age_band, gender, count(attrition) as attrition,
round((cast(count(attrition) as numeric) / (select count(attrition) from hrdata where attrition = 'Yes')) * 100,2) as pct
from hrdata
where attrition = 'Yes' and department='Sales'
group by age_band, gender
order by age_band, gender desc;
--11) Job Satisfaction Rating-
--Data Validation:
-- Create a temporary table to store the pivot query result
CREATE TABLE #TempPivot
(
job_role varchar(50),
[1] numeric,
[2] numeric,
[3] numeric,
[4] numeric
);
-- Insert data into the temporary table using conditional aggregation
INSERT INTO #TempPivot (job_role, [1], [2], [3], [4])
SELECT job_role,
SUM(CASE WHEN job_satisfaction = 1 THEN employee_count ELSE 0 END) AS [1],
SUM(CASE WHEN job_satisfaction = 2 THEN employee_count ELSE 0 END) AS [2],
SUM(CASE WHEN job_satisfaction = 3 THEN employee_count ELSE 0 END) AS [3],
SUM(CASE WHEN job_satisfaction = 4 THEN employee_count ELSE 0 END) AS [4]
FROM hrdata
GROUP BY job_role;
select* from #TempPivot
--Functional Validation:
-- Create a temporary table to store the pivot query result
CREATE TABLE #TempPivot
(
job_role varchar(50),
[1] numeric,
[2] numeric,
[3] numeric,
[4] numeric
);
-- Insert data into the temporary table using conditional aggregation
INSERT INTO #TempPivot (job_role, [1], [2], [3], [4])
SELECT job_role,
SUM(CASE WHEN job_satisfaction = 1 THEN employee_count ELSE 0 END) AS [1],
SUM(CASE WHEN job_satisfaction = 2 THEN employee_count ELSE 0 END) AS [2],
SUM(CASE WHEN job_satisfaction = 3 THEN employee_count ELSE 0 END) AS [3],
SUM(CASE WHEN job_satisfaction = 4 THEN employee_count ELSE 0 END) AS [4]
FROM hrdata
where gender='Female'
GROUP BY job_role;
select* from #TempPivot
--12) Age-wise, Gender-wise Employee count (Power Bi stacked bar chart) -
--Data Validation:
select age_band,gender,sum(employee_count) from hrdata
group by age_band,gender
order by age_band,gender desc
--Functional Validation:
select age_band,gender,sum(employee_count) from hrdata
where department='Sales'
group by age_band,gender
order by age_band,gender desc