2011-01-26 101 views
0

我正在嘗試輸出訂單的每日,每週,每月和每年的計數以及這些時間範圍之間這些訂單的總金額。目前我已經提出了以下幾點。它的很長一段時間,但它只是一個複製和粘貼的工作,同樣的聲明只是稍微修改,以年,月,周和日爲一組。每行分組日期範圍

SELECT YEAR(s.LastUpdated) AS 'Year', 0 AS 'Month', 0 AS 'Week', 0 AS 'Day', count(rowId) AS 'Transactions', ROUND(SUM(Amount),2) AS 'Partner Cut' 
FROM gfw_orders AS o 
JOIN gfw_sage_orders AS s 
ON o.sageRef = s.VendorTxCode 
WHERE YEAR(s.LastUpdated) = 2011 
GROUP BY 
YEAR(s.LastUpdated) 
    UNION 
SELECT YEAR(s.LastUpdated) AS 'Year', MONTHNAME(s.LastUpdated) AS 'Month', 0 AS 'Week', 0 AS 'Day', count(rowId) AS 'Transactions', ROUND(SUM(Amount),2) AS 'Partner Cut' 
FROM gfw_orders AS o 
JOIN gfw_sage_orders AS s 
ON o.sageRef = s.VendorTxCode 
WHERE MONTH(s.LastUpdated) = 1 
GROUP BY 
YEAR(s.LastUpdated), MONTH(s.LastUpdated) 
    UNION 
SELECT YEAR(s.LastUpdated) AS 'Year', MONTHNAME(s.LastUpdated) AS 'Month', WEEKOFYEAR(s.LastUpdated) AS 'Week', 0 AS 'Day', count(rowId) AS 'Transactions', ROUND(SUM(Amount),2) AS 'Partner Cut' 
FROM gfw_orders AS o 
JOIN gfw_sage_orders AS s 
ON o.sageRef = s.VendorTxCode 
WHERE WEEK(s.lastUpdated) = 4 
GROUP BY 
YEAR(s.LastUpdated), MONTH(s.LastUpdated), WEEK(s.LastUpdated) 
    UNION 
SELECT YEAR(s.LastUpdated) AS 'Year', MONTHNAME(s.LastUpdated) AS 'Month', WEEKOFYEAR(s.LastUpdated) AS 'Week', DAY(s.lastUpdated), count(rowId) AS 'Transactions', ROUND(SUM(Amount),2) AS 'Partner Cut' 
FROM gfw_orders AS o 
JOIN gfw_sage_orders AS s 
ON o.sageRef = s.VendorTxCode 
WHERE DAY(s.LastUpdated) = 26 
GROUP BY 
YEAR(s.LastUpdated), MONTH(s.LastUpdated), WEEK(s.LastUpdated), DAY(s.LastUpdated) 

,輸出

Year  Month  Week Day Count Amount 
2011 0    0  0  3  285.00 
2011 January  0  0  3  285.00 
2011 January  4  0  2  190.00 
2011 January  4  26  1  95.00 

,這是提供能相應地輸出的年,月,周,日數和交易金額一個理想的輸出。但是,這只是一種類型的訂單。在訂單表中有兩種類型的訂單,在線和終端,理想情況下,我想在同一個查詢中輸出這兩種類型,而不是多次查詢數據庫。

我有兩個問題,我遇到了問題。首先,是否有一種更簡單的方法來實現相同的結果?我不希望過於複雜,因爲導致第二個問題,因爲一些訂單不同,我希望結果顯示在線訂單和終端訂單,如果我只是複製和UNION我已經添加的' WHERE終端= 1'因爲聯盟結果是相同的。有沒有辦法在同一個查詢中輸出兩組輸出?

我很可能過分複雜化了我的目標,但事先感謝您的任何建議或指示。

回答

0

可以嘗試像這樣的例子,通過使用GROUP BY WITH ROLLUP

mysql> SELECT year, country, product, SUM(profit) 
    -> FROM sales 
    -> GROUP BY year, country, product WITH ROLLUP; 

結果將是

+------+---------+------------+-------------+ 
| year | country | product | SUM(profit) | 
+------+---------+------------+-------------+ 
| 2000 | Finland | Computer |  1500 | 
| 2000 | Finland | Phone  |   100 | 
| 2000 | Finland | NULL  |  1600 | 
| 2000 | India | Calculator |   150 | 
| 2000 | India | Computer |  1200 | 
| 2000 | India | NULL  |  1350 | 
| 2000 | USA  | Calculator |   75 | 
| 2000 | USA  | Computer |  1500 | 
| 2000 | USA  | NULL  |  1575 | 
| 2000 | NULL | NULL  |  4525 | 
| 2001 | Finland | Phone  |   10 | 
| 2001 | Finland | NULL  |   10 | 
| 2001 | USA  | Calculator |   50 | 
| 2001 | USA  | Computer |  2700 | 
| 2001 | USA  | TV   |   250 | 
| 2001 | USA  | NULL  |  3000 | 
| 2001 | NULL | NULL  |  3010 | 
| NULL | NULL | NULL  |  7535 | 
+------+---------+------------+-------------+ 

對於Rerence與ROLLUP

您的查詢是相當大的,這就是爲什麼我選擇了放棄另一例!