2013-01-25 119 views
2

我有一張表格,裏面填寫了銷售汽車的數據。MySQL數據彙總平均值失敗

我想得到每日平均數,所以我使用聚合和下面的函數。

SELECT 
     DAYOFMONTH(TIMESTAMP) AS DAY, 
     MONTH(TIMESTAMP) AS MONTH, 
     AVG(number_sold) AS sold, 
     AVG(amount_earned) AS earned, 
     AVG(wages_spent) AS wages 
FROM cardata 
GROUP BY DAYOFMONTH(TIMESTAMP) 
ORDER BY TIMESTAMP DESC 
LIMIT 0, 12; 

我應該得到12行,每天有1行。 12行返回,但每天都會出現問題。 2月份我只獲得第5天和第8天。

我的陳述有問題嗎?

請幫忙嗎?

回答

1

我相信GROUPBY並不在這裏。

嘗試:

GROUP BY DAYOFMONTH(TIMESTAMP) , MONTH(TIMESTAMP) 

這應該組你怎麼需要它的數據。

或者你可以這樣做:

SELECT 
    DATE_FORMAT (TIMESTAMP, '%m/%d') AS DAY, 
    ... 
FROM cardata 
GROUP BY DAY 
ORDER BY TIMESTAMP DESC 
LIMIT 0, 12 

但固定GROUPBY是應該修復它最主要的。

+0

嘿感謝團體修復它,沒有意識到我可以做到這一點 –