2012-12-20 145 views
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我的結果,但我有這個錯誤SQL查詢錯誤

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. 
+2

這是完整的查詢嗎? – imreal

回答

1

包括在SelectGroup by

+0

感謝您的回答。這很有幫助 – apkos99

+0

@ apkos99:我的榮幸! – Kaf

2

有什麼不清楚的錯誤消息?您正嘗試按未彙總的內容進行排序。下面是一種解決方案:

order by max(d.Functio) 

問題是別名「job」未知。您需要使用分配給表格的「d」。

這應該工作,因爲大概有一個job.Functio每個工作id。另一種解決方法是改變group by到:

group by e.Job_ID, d.Job.Functio 
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 

否則在您需要申請訂貨像分鐘(),MAX(),AVG()等的聚集函數