1
select E.Job_ID, count(*), max(Employee_Salary) as kos, avg(Employee_Salary)
    from Employee E  
    inner join JOB D on E.Job_ID = D.Job_ID
    group by E.Job_ID

我想通过 D.Functio 订购我的结果,但我有这个错误

Msg 8120, Level 16, State 1, Line 1
Column 'JOB.Functio' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
4

3 回答 3

2

错误消息有什么不清楚的地方?您正在尝试按未聚合的内容进行排序。这是一种解决方案:

order by max(d.Functio)

问题是别名“job”未知。您需要使用分配给表格的“d”。

这应该可行,因为大概有一个 job.Functio 每个作业 ID。另一个解决方法是将其更改group by为:

group by e.Job_ID, d.Job.Functio
于 2012-12-20T20:53:20.497 回答
1

如果它是描述性列(工作职能?),您想将其添加到您的 GROUP BY

select E.Job_ID, count(*), max(Employee_Salary) as kos, avg(Employee_Salary)
    from Employee E  
    inner join JOB D on E.Job_ID = D.Job_ID
    group by E.Job_ID, D.Functio
    order by D.Functio

否则,在排序中,您需要应用一个聚合函数,如 min()、max()、avg() 等。

于 2012-12-20T20:55:06.767 回答
1

包括D.FunctioSelectGroup by

select E.Job_ID, D.Functio,
     count(*), max(Employee_Salary) as kos, avg(Employee_Salary)
from Employee E  
inner join JOB D on E.Job_ID = D.Job_ID
group by E.Job_ID, D.Functio
order by D.Functio
于 2012-12-20T20:55:21.403 回答