2015-07-01 28 views
0

我使用MySQL的..SQL - 查找最高的銷售起始日期範圍的單日

我有一個簡單的銷售表如下:

o----o----------o-----------o 
| id | store_id | logDate | 
o----o----------o-----------o 
| 1 | 1  | 2015-1-13 | 
| 2 | 1  | 2015-1-14 | 
| 3 | 2  | 2015-1-11 | 
| 4 | 2  | 2015-1-18 | 
o----o----------o-----------o 

和銷售產品表

o----o----------o---------o------------o 
| id | sale_id | qty | price | 
o----o----------o---------o------------o 
| 1 | 1  |  1 |   10 | 
| 2 | 2  |  1 |   10 | 
| 3 | 2  |  1 |   10 | 
| 4 | 3  |  1 |   10 | 
| 5 | 3  |  1 |   10 | 
| 6 | 3  |  1 |   10 | 
| 7 | 4  |  1 |   10 | 
| 8 | 4  |  1 |   10 | 
o----o----------o---------o------------o 

預期結果

o-- --------o----------------o---------------------o 
| store_id | SUM(price*qty) | Highest Date On | 
o-----------o----------------o---------------------o 
|  1  |    20 |   2015-1-14 | 
|  2  |    30 |   2015-1-11 | 
O-----------o----------------o---------------------o 

如何達到我的預期效果? 我曾嘗試如下,但預期它沒有工作:

SELECT store_id, MAX(total), highestSingleDateOn 
FROM (
    SELECT SUM(price * qty) AS total, 
     DATE(s.logDate) AS highestSingleDateOn, s.store_id AS store_id 
    FROM sale_product sp JOIN sales s ON s.id = sp.sales_id 
    GROUP BY DATE(s.logDate), s.store_id 
    ORDER BY DATE(s.logDate) ASC 
) AS result_for_highest_single_day 
GROUP BY highestSingleDateOn, store_id 

回答

0
SELECT store_id, MAX(total), highestSingleDateOn 
FROM (
    SELECT SUM(price * qty) AS total, 
     DATE(s.logDate) AS highestSingleDateOn, s.store_id AS store_id 
    FROM sale_product sp JOIN sales s ON s.id = sp.sales_id 
    GROUP BY DATE(s.logDate), s.store_id 
    ORDER BY total DESC 
) AS result_for_highest_single_day 
GROUP BY store_id 

我只是修改了劇本ORDER BY DATE(s.logDate) ASC >>ORDER BY total DESCGROUP BY highestSingleDateOn, store_id >>GROUP BY store_id

*上面的sql腳本,它使用了關於MYSQL的group by的不穩定的功能。

*然後根據Mysql標準,我寫了一個其他版本的sql腳本。

select table1.* 
from 
(SELECT SUM(price * qty) AS total, 
     DATE(s.logDate) AS highestSingleDateOn, s.store_id AS store_id 
    FROM sale_product sp JOIN sales s ON s.id = sp.sale_id 
    GROUP BY DATE(s.logDate), s.store_id) as table1 
, 
(select tmp.store_id,MAX(tmp.total) as max_total from 
     (SELECT SUM(price * qty) AS total, 
    DATE(s.logDate) AS highestSingleDateOn, s.store_id AS store_id 
    FROM sale_product sp JOIN sales s ON s.id = sp.sale_id 
    GROUP BY DATE(s.logDate), s.store_id) as tmp group by tmp.store_id) as table2 

where table1.store_id = table2.store_id and table1.total=table2.max_total 
+0

這個答案使用MySQL的(錯誤)功能,允許不屬於GROUP BY('highestSingleDateOn'列)的SELECT中的列。 MySQL *顯式警告不要使用此功能(https://dev.mysql.com/doc/refman/5.6/en/group-by-handling.html)。還有其他方法可以解決不依賴於*文檔*不穩定的語言特性的問題。 –

+0

@GordonLinoff謝謝你的提醒。我修改了答案。 – Maxy

+0

。 。修改後的版本應該使用顯式的'join',而不是''where''子句中條件的隱式'join'。 –

0

一種方式在MySQL做,這是多一個聚合,然後聯接。也許一個更簡單的方法是使用變量:

SELECT sd.* 
FROM (SELECT sd.*, 
      (@rn := if(@s = store_id, @rn + 1, 
         if(@s := store_id, 1, 1) 
         ) 
      ) as rn 
     FROM (SELECT DATE(s.logDate) AS date, s.store_id, SUM(price * qty) AS total 
      FROM sale_product sp JOIN sales s ON s.id = sp.sales_id 
      GROUP BY DATE(s.logDate), s.store_id 
      ORDER BY s.store_id, total desc 
      ) sd cross join 
      (SELECT @rn := 0, @s := -1) params 
    ) sd 
WHERE rn = 1; 
+0

你介意給我解釋一下額外的'rn'列嗎?我知道這是一個輔助變量,但背後的理由是什麼? – exodream

+0

另外我不認爲查詢工作..我已經測試了真實的數據。它沒有按預期工作。 – exodream

+0

@exodream。 。 。最初的答案返回了銷售的第一天。我一定誤會了這個問題。您通過更改'order by'子句獲得最高銷售額。 –