假设我有一个表,它包含以下列:
assignment_id,employee.id,employee_salary和employee_performance
(assignment_id - int,employee.id - int,employee_salary - int,employee_performance - varchar)。
employee_performance列包含以下值:excellent,good,average,bad,null。 我想为这些值分配整数,并计算不同工作中员工的平均绩效。我怎么能这样做?
例如,一些员工完成了两项任务,结果是:优秀和良好。我将10分为优秀分配,9分为良好分配,平均分为9分。
assignment_id, employee.id, employee_salary employee_performance 1 1 100,000 excellent 2 1 100,000 excellent 3 1 100,000 good 4 4 50,000 good 5 3 75,000 null
Null means that an assignment is not yet completed.
我想将整数分配给employee_performance。例如,优秀 - 10,好 - 9等
结果应该:
eployee.id average_performance 1 9,7 3 9
epmployee.id = 4不包括在内,因为他没有完成作业。
答案 0 :(得分:0)
您可以使用case
。像这样:
select employee_id,
avg(case when employee_performance = 'excellent' then 4.0
when employee_performance = 'good' then 3.0
when employee_performance = 'average' then 2.0
when employee_performance = 'bad' then 1.0
end) as avg_performance
from table
group by employee_id;
答案 1 :(得分:0)
您可以创建一个包含每个性能(即优秀,良好等)及其相应值的2列表。
------------------------------
|performance | value|
------------------------------
|excellent | 10 |
------------------------------
|good | 9 |
------------------------------
拥有该表后,您可以进行简单的加入和分组,以获得每位员工的平均价值。
select id, avg(pv.value)
from employees e
left join performance_values pv
on pv.performance = e.employee_performance
group by id