我想计算我的SQL表中每日销售额的平均值。
我尝试使用以下查询,但它会出现错误,提示“在select关键字附近语法不正确”。
select avg(select sum(total) from canteen group by date) as average from canteen
select avg(select sum(total) from canteen group by date) as average from canteen
select day, sum(total)
from canteen
group by day;
select avg(total)
from (select day, sum(total) as total
from canteen
group by day
) c;
select sum(total) / count(distinct day)
from canteen;
正确的语法是
SELECT date, avg(total) average FROM (
SELECT date, sum(total)total FROM canteen GROUP BY DATE
)AS A
grouP BY date
如果可以的话,我希望你不要担心嵌套查询;
select avg(total) from(select sum(total) as total from canteen group by date) as T
最好尝试使用像这样的子查询:
select avg(total) from(select sum(total) from canteen group by date)