2016-03-15 137 views
0

我有問題.. 我有這樣的數據庫大多是這樣的, 我怎麼能得到每個客戶每週平均花費?你能告訴我這個SQL查詢嗎?查詢獲得每週平均花費每個客戶

這裏的例子

| customer | spending | date    | 
| -----------|----------|---------------------| 
| customer A |  105 | 2016-03-02 11:24:38 | 
| customer A |  75 | 2016-02-02 11:24:38 | 
| customer B |  35 | 2016-03-02 11:24:38 | 
| customer A |  40 | 2016-03-02 11:24:38 | 
| customer B |  105 | 2016-02-15 11:24:38 | 
| customer A |  100 | 2016-03-02 11:24:38 | 
| customer A |  150 | 2016-02-21 11:24:38 | 
| customer A |  139 | 2016-03-02 11:24:38 | 

更多...

任何人都可以幫我嗎?感謝

回答

2

嘗試在一年的客戶和星期的組合使用GROUP BY

SELECT customer, WEEK(date), AVG(spending) 
FROM sales 
GROUP BY customer, WEEK(date) 
+0

感謝先生..它工作 –