0% completed
Problem Statement
Table: Performance
Each row in this table represents a performance evaluation of an employee, detailing the employee's name, the evaluation result, the evaluation round, and the performance score.
+----------------+---------+
| Column Name | Type |
+----------------+---------+
| employee_name | varchar |
| evaluation | varchar |
| round | int |
| score | int |
+----------------+---------+
This table may have duplicate rows.
This table contains information collected from performance evaluations of employees.
The round column has a value from 1 to 10.
The score column has a value from 1 to 100. An evaluation with a score less than 50 is considered a poor evaluation.
Develop a solution to find each employee_name
, their performance, and improvement_rate
.
We define employee performance as:
- The average of the ratio between employee evaluation score and its round.
We also define improvement rate as:
- The percentage of all evaluations with a score improvement over the previous rounds, and are greater than 50.
Both performance and improvement_rate
should be rounded to 2 decimal places.
Return the result table in any order.
Example
Input:
Performance table: +---------------+-------------+-------+-------+ | employee_name | evaluation | round | score | +---------------+-------------+-------+-------+ | Alice | Efficiency | 1 | 80 | | Alice | Productivity| 2 | 90 | | Alice | Creativity | 3 | 60 | | Bob | Efficiency | 1 | 40 | | Bob | Productivity| 2 | 60 | | Bob | Creativity | 3 | 70 | +---------------+-------------+-------+-------+
Output:
+---------------+-----------+-------------------+ | employee_name | performance | improvement_rate | +---------------+-----------+-------------------+ | Alice | 48.33 | 33.33 | | Bob | 31.11 | 66.67 | +---------------+-----------+-------------------+
Output Explanation
Step 1: Calculate Employee Performance
For each employee:
- Divide the
score
by theround
for all evaluations. - Calculate the average of these ratios.
- Alice:
\text{Performance} = \frac{80}{1} + \frac{90}{2} + \frac{60}{3} = 80 + 45 + 20 = 145
\text{Average} = \frac{145}{3} = 48.33
- Bob:
\text{Performance} = \frac{40}{1} + \frac{60}{2} + \frac{70}{3} = 40 + 30 + 23.33 = 93.33 \text{Average} = \frac{93.33}{3} = 31.11
Step 2: Calculate Improvement Rate
For each employee:
-
Check if the score in each round:
- Is greater than 50.
- Is greater than the score in the previous round.
-
Count the number of evaluations that meet the criteria.
-
Alice:
- Round 1:
80
(No previous round, does not count). - Round 2:
90
(Improved and >50). - Round 3:
60
(Not improved but >50). - Total: 1 improvement out of 3 evaluations. \text{Improvement Rate} = \frac{1}{3} \times 100 = 33.33
- Round 1:
-
Bob:
- Round 1:
40
(No previous round, does not count). - Round 2:
60
(Improved and >50). - Round 3:
70
(Improved and >50). - Total: 2 improvements out of 3 evaluations.
- Round 1:
\text{Improvement Rate} = \frac{2}{3} \times 100 = 66.67
Try It Yourself
Solution
To solve this problem, we need to calculate each employee's performance and improvement rate based on their evaluation scores across different rounds. The approach involves systematically breaking down the problem into manageable steps, utilizing SQL window functions and aggregation techniques to derive the required metrics.
- Calculate Performance: Determine the average ratio of each employee's score to the corresponding evaluation round.
- Determine Improvement Rate: Identify the percentage of evaluations where an employee's score improved compared to the previous round and is greater than 50.
- Round the Results: Ensure that both performance and improvement rate are rounded to two decimal places for precision.
SQL Query
WITH LaggedPerformance AS ( SELECT employee_name, round, score, LAG(score) OVER (PARTITION BY employee_name ORDER BY round) AS previous_score FROM Performance ) SELECT employee_name, ROUND(AVG(score * 1.0 / round), 2) AS performance, ROUND( (SUM(CASE WHEN score > 50 AND previous_score IS NOT NULL AND score > previous_score THEN 1 ELSE 0 END) * 100.0 / COUNT(*)), 2 ) AS improvement_rate FROM LaggedPerformance GROUP BY employee_name;
Step-by-Step Approach
Step 1: Calculate Performance
Compute the average of the ratio between each employee's score and the evaluation round to assess overall performance.
SQL Query:
SELECT employee_name, AVG(score * 1.0 / round) AS performance FROM Performance GROUP BY employee_name;
Explanation:
AVG(score * 1.0 / round)
:- Calculates the average of the score divided by the round number for each employee.
- Multiplying by
1.0
ensures that the division is performed using floating-point arithmetic, preserving decimal values.
GROUP BY employee_name
:- Aggregates the calculations for each employee individually.
Output After Step 1:
+---------------+-------------+ | employee_name | performance | +---------------+-------------+ | Alice | 48.3333333 | | Bob | 31.111111 | +---------------+-------------+
Step 2: Determine Improvement Rate
Calculate the percentage of evaluations where an employee's score improved compared to the previous round and is greater than 50.
SQL Query:
WITH LaggedPerformance AS ( SELECT employee_name, round, score, LAG(score) OVER (PARTITION BY employee_name ORDER BY round) AS previous_score FROM Performance ) SELECT employee_name, ROUND( (SUM(CASE WHEN score > 50 AND previous_score IS NOT NULL AND score > previous_score THEN 1 ELSE 0 END) * 100.0 / COUNT(*)), 2 ) AS improvement_rate FROM LaggedPerformance GROUP BY employee_name;
Explanation:
WITH LaggedPerformance AS (...)
:- Creates a Common Table Expression (CTE) that adds a column
previous_score
representing the score from the previous round for each employee.
- Creates a Common Table Expression (CTE) that adds a column
LAG(score) OVER (PARTITION BY employee_name ORDER BY round)
:- Retrieves the score from the preceding round for each employee, enabling comparison between consecutive rounds.
SUM(CASE WHEN ... THEN 1 ELSE 0 END)
:- Counts the number of evaluations where:
- The current
score
is greater than 50. - There is a valid
previous_score
(i.e., not the first round). - The current
score
is higher than theprevious_score
.
- The current
- Counts the number of evaluations where:
(SUM(...) * 100.0 / COUNT(*))
:- Calculates the improvement rate as a percentage by dividing the count of improved evaluations by the total number of evaluations for each employee.
ROUND(..., 2)
:- Rounds the improvement rate to two decimal places.
Output After Step 2:
+---------------+-------------------+ | employee_name | improvement_rate | +---------------+-------------------+ | Alice | 33.33 | | Bob | 66.67 | +---------------+-------------------+
Step 3: Combine Performance and Improvement Rate
Objective:
Merge the performance and improvement rate calculations to present a consolidated view for each employee.
SQL Query:
WITH LaggedPerformance AS ( SELECT employee_name, round, score, LAG(score) OVER (PARTITION BY employee_name ORDER BY round) AS previous_score FROM Performance ) SELECT employee_name, ROUND(AVG(score * 1.0 / round), 2) AS performance, ROUND( (SUM(CASE WHEN score > 50 AND previous_score IS NOT NULL AND score > previous_score THEN 1 ELSE 0 END) * 100.0 / COUNT(*)), 2 ) AS improvement_rate FROM LaggedPerformance GROUP BY employee_name;
Explanation:
- Combining CTE and Aggregations:
- The CTE
LaggedPerformance
is used to access both the current and previous scores within the same query.
- The CTE
- Calculating Performance and Improvement Rate Simultaneously:
AVG(score * 1.0 / round)
computes the performance metric.- The
SUM(CASE ...)
construct calculates the number of improved evaluations, which is then converted into a percentage to represent the improvement rate.
- Rounding the Results:
- Both
performance
andimprovement_rate
are rounded to two decimal places for clarity and consistency.
- Both
Final Output:
+---------------+-------------+-------------------+ | employee_name | performance | improvement_rate | +---------------+-------------+-------------------+ | Alice | 48.33 | 33.33 | | Bob | 31.11 | 66.67 | +---------------+-------------+-------------------+
.....
.....
.....
Table of Contents
Contents are not accessible
Contents are not accessible
Contents are not accessible
Contents are not accessible
Contents are not accessible