2014-04-16 42 views
0

我目前正在編寫一個SQL語句,它要求我列出工作時間超過平均值的工作人員。 這裏是我當前的代碼:使用必須平衡

Select T.TotalCandy,Sum(T.Amountofcandy) "Total Candy" 
From ToralChocolate 
Inner Join Ompaloompa O 
On T.CandyNo = O.CandyNo and T.BoatNO = O.BoatNo 
Inner Join Transport TR 
On TR.Transport = T.Transport 
Having Sum(T.AmountofCandy) > (Select Avg(AmountofCandy) from TotalChocolate) 
Group By T.TotalCandy 
Order By 1; 

我在有聲明的子查詢碰到我的問題,因爲我比較它的平均工作時間不是工作小時數的總和的平均值!

任何幫助,將不勝感激。

回答

0
Select W.WrkName,Sum(TI.HrsWorked) "Total Hours" 
From Timelog TI 
Inner Join Allocation A 
On TI.WrkID = A.WrkID and TI.TaskID = A.TaskID 
Inner Join Worker W 
On TI.WrkID = W.WrkID 
Group By W.WrkName 
Having Sum(TI.HrsWorked) > 
    (Select Avg(HrsWorked) 
    From 
     (Select WrkID, SUM(HrsWorked) As HrsWorked 
     From Timelog 
     Group By WrkID) 
    ) 
Order By 1; 

你試過類似的東西嗎?

+0

是的,這是完美的,我試過一個嵌套的子查詢之前,必須有一個語法錯誤,因爲它沒有工作,所以我認爲他們不可能-_-。非常感謝! – Kaleon

+0

歡迎您! :) – Guneli

1
Select W.WrkName,Sum(TI.HrsWorked) "Total Hours" 
From Timelog TI 
Inner Join Allocation A 
On TI.WrkID = A.WrkID and TI.TaskID = A.TaskID 
Inner Join Worker W 
On TI.WrkID = W.WrkID 
Group By W.WrkName 
Having Sum(TI.HrsWorked) > (Select Avg(HrsWorked) from Timelog) 
Order By 1; 

在組後有條款!