如何找到按工作日分组的利润平均值,而不是工作日内的平均利润

时间:2015-10-16 23:28:30

标签: sql syntax average weekday

我的代码有什么问题?如果我在最后一组之前和之后删除代码,则平均每个工作日内的销售。但我希望平均所有工作日。

select  [Weekday], Avg(Amountpaid)
from (select  Sum(Amountpaid), [Weekday], [Date]
      from 
           itsthegoodtable

      group by [Weekday], [Date]
     )

group by [Weekday]

它不断带来错误信息

  

关键字组附近的语法不正确

2 个答案:

答案 0 :(得分:1)

你快到了。您的查询缺少子查询的别名。

select [Weekday], Avg(Amountpaid) 
from (select Sum(Amountpaid) as amountpaid, [Weekday], [Date] from itsthegoodtable
group by [Weekday], [Date] ) t
group by [Weekday]

答案 1 :(得分:0)

您需要为使用总和计算的值添加别名:

select  [Weekday], Avg(Amountpaid) as Amountpaid
from (select  Sum(Amountpaid) as Amountpaid, [Weekday], [Date]
      from 
           itsthegoodtable

      group by [Weekday], [Date]
     ) ap

group by [Weekday]