使用必须找到平均值

时间:2014-04-16 05:31:03

标签: sql sum aggregate-functions average

我目前正在撰写一份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;

我在with语句中的子查询中遇到了我的问题,因为我将它与工作时的平均工作时间进行比较,而不是工作小时数的平均值!

任何帮助都将不胜感激。

2 个答案:

答案 0 :(得分: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 Timelog)
Order By 1;

在团队之后拥有条款!

答案 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;

你有没有尝试过类似的东西?