2013-03-28 82 views
0

因此,我有一個表格,其中包含一組訂購商品,訂購商品表格記錄特定訂單中包含的特定商品的數量。以下是該示例中使用的表格的簡化版本。MySQL顯示總和0

訂單項目表

order_item_id : int 
order_item_item_id : int 
order_item_quantity : int 
order_item_order_id : int 

Order表

order_id : int 
order_date : date 

我試圖執行一個查詢,將平均特定order_item_item_id的銷售上,每週,每月和所有的時間基礎。

目前我的查詢如下

SELECT totalAvg.item 

ROUND(AVG(totalAvg.total)+0.4999999, 0) AS totalAverage, 
ROUND(AVG(monthAvg.total)+0.4999999, 0) AS monthAverage, 
ROUND(AVG(weekAvg.total)+0.4999999, 0) AS weekAverage from 

(SELECT orderitem.order_item_item_id AS item, SUM(orderitem.order_item_quantity) AS total, 
DATE(`order`.order_date) AS date FROM orderitem, `order` WHERE orderitem.order_item_order_id = 
`order`.order_id GROUP BY DATE(`order`.order_date), order_item_item_id) AS totalAvg, 

(SELECT orderitem.order_item_item_id AS item, SUM(orderitem.order_item_quantity) AS total, 
DATE(`order`.order_date) AS date FROM orderitem, `order` WHERE orderitem.order_item_order_id = 
`order`.order_id AND `order`.order_date > date_sub(now(), interval 1 week) GROUP BY 
DATE(`order`.order_date), order_item_item_id) AS weekAvg, 

(SELECT orderitem.order_item_item_id AS item, SUM(orderitem.order_item_quantity) AS total, 
DATE(`order`.order_date) AS date FROM orderitem, `order` WHERE orderitem.order_item_order_id = 
`order`.order_id AND `order`.order_date > date_sub(now(), interval 1 month) GROUP BY 
DATE(`order`.order_date), order_item_item_id) AS monthAvg, 

WHERE totalAvg.item = monthAvg.item AND 
monthAvg.item = weekAvg.item 
GROUP BY item 

這樣做的問題是,如果一個項目沒有在weekAvg表存在,那麼沒有結果打印出來totalAvg或monthAvg。我怎麼能在一個陳述中做這件事?

的樣本數據

{order_id : 5, order_date : 02/02/2013} 
{order_id : 6, order_date : 13/03/2013} 

{order_item_id : 1, order_item_order_id : 5, order_item_item_id : 1, order_item_quantity : 3} 
{order_item_id : 2, order_item_order_id : 6, order_item_item_id : 1, order_item_quantity : 4} 

電流輸出將返回,因爲每週的報告沒有什麼不具備的訂單項ID爲1.我試圖解決這個問題的條目,從而它將woudl輸出下面

totalAverage = 3.5 
monthAverage = 4 
weekAverage = 0 

的值被計算如下:

總平均=總NUM銷售特定商品的銷售額除以銷售商品的天數。

月份和星期值與先前的計算值相同,但具有時間限制,因此訂單必須放置在最後一週/月內。

回答

0

您需要使用outer joins才能將totalAvg連接到monthAvg和weekAvg。你可以使用COALESCE()來確保你得到0而不是空的星期或月份值。

您還應該使用ANSI連接語法。它不易出錯並且更具可讀性。

嘗試這樣:

SELECT totalAvg.item, 
ROUND(AVG(totalAvg.total)+0.4999999, 0) AS totalAverage, 
COALESCE(ROUND(AVG(monthAvg.total)+0.4999999, 0),0) AS monthAverage, 
COALESCE(ROUND(AVG(weekAvg.total)+0.4999999, 0),) AS weekAverage 
from 
(
    SELECT orderitem.order_item_item_id AS item, 
    SUM(orderitem.order_item_quantity) AS total, 
    DATE(`order`.order_date) AS date 
    FROM orderitem 
    inner join `order` on orderitem.order_item_order_id = `order`.order_id 
    GROUP BY DATE(`order`.order_date), order_item_item_id 
) AS totalAvg 
left outer join 
(
    SELECT orderitem.order_item_item_id AS item, 
    SUM(orderitem.order_item_quantity) AS total, 
    DATE(`order`.order_date) AS date 
    FROM orderitem 
    inner join `order` on orderitem.order_item_order_id = `order`.order_id 
    WHERE `order`.order_date > date_sub(now(), interval 1 week) 
    GROUP BY 
    DATE(`order`.order_date), order_item_item_id 
) AS weekAvg on weekAvg.item = totalAvg.item 
left outer join 
(
    SELECT orderitem.order_item_item_id AS item, 
    SUM(orderitem.order_item_quantity) AS total, 
    DATE(`order`.order_date) AS date 
    FROM orderitem 
    inner join `order` on orderitem.order_item_order_id = `order`.order_id 
    WHERE `order`.order_date > date_sub(now(), interval 1 month) 
    GROUP BY DATE(`order`.order_date), order_item_item_id 
) AS monthAvg on monthAvg.item = totalAvg.item 
GROUP BY item 
+0

非常感謝,我現在試試這個。 – 2013-03-28 21:13:33

+0

我覺得我很困惑,可能是因爲我去了一個臃腫(儘管減少)的例子。現在我試圖把它放到實際的版本中,並試圖弄清楚你已經做了什麼,但是不能讓它起作用。你能否展示一個Coallese,內連接和左外連接的例子,同時解釋它的作用/工作原理?我爲了我的生活無法解決這個問題,至少可以說是令人沮喪:P – 2013-03-28 21:37:01