比如有一个HR的数据库,相关的表有两个:employee
和 performance
,employee
表就是员工的所有信息比如有department
,performance
表的结构如下:
performanceRating | performanceDescriptor |
---|---|
1 | Low |
2 | Good |
3 | Excellent |
4 | Outsanding |
performanceRating
在performance
表中为 primary key,在employee
表中为 foreign key
如果需要知道每个部门的平均 PerformanceRaing,并按从小到大的顺序排列,则为:
SELECT Avg(employee.PerformanceRating) AS [Average Performance Raing], employee.Department
FROM employee
GROUP BY employee.Department
ORDER BY Avg(employee.PerformanceRating);
输出结果为:
Average Performance Raing | Department |
---|---|
3.14655172413793 | Sales |
3.15514018691589 | Research & Development |
3.21212121212121 | Human Resources |
但是这样计算出的部门平均 performanceRating 不为整数,但是如果也需要在平均值旁附上performance的描述,即把performace
表中的描述也跟在后面,不是仅仅展现出数字而已,应该如何操作呢,应该不能直接 INNER JOIN performance表吧?
比如的输出结果为:
Department | Average Performance Raing | PerformanceDescriptor |
---|---|---|
Sales | 3.14655172413793 | 应该为Excellent? |
Research & Development | 3.15514018691589 | 应该为Excellent? |
Human Resources | 3.21212121212121 | 应该为Excellent? |
可以取整再去关联