我想從我的sql表中計算銷售額的每日平均值。 我嘗試使用下面的查詢,但它給錯誤說「關鍵字附近有語法錯誤選擇」從sql表中計算每日平均銷售額
select avg(select sum(total) from canteen group by date) as average from canteen
我想從我的sql表中計算銷售額的每日平均值。 我嘗試使用下面的查詢,但它給錯誤說「關鍵字附近有語法錯誤選擇」從sql表中計算每日平均銷售額
select avg(select sum(total) from canteen group by date) as average from canteen
不嵌套聚合函數。這是你想獲得每個日期的總和是什麼:
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 day, sum(total)
from canteen
group by day;
您可以使用子查詢得到的平均正確的語法是
SELECT date, avg(total) average FROM (
SELECT date, sum(total)total FROM canteen GROUP BY DATE
)AS A
grouP BY date
這是行不通的。語法錯誤 –
現在檢查@MohitJoshi我錯過了組通過 –
我pre如果你不打擾嵌套查詢,
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)
你運行此查詢在DBMS或Java代碼?如果在代碼中你應該把它放在你的問題 –