每行分组日期范围

时间:2011-01-26 10:58:17

标签: mysql date group-by

我正在尝试输出这些时间范围之间的每日,每周,每月和每年的订单数量以及这些订单的总金额。目前我已经提出以下建议。它很长,但它只是同一个声明的复制和粘贴工作,只是稍微修改为按年,月,周和日分组。

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 Terminal = 1',因为结合,结果是相同的。有没有办法在同一个查询中输出两组输出?

我可能过度复杂化了我的目标,但提前感谢您的任何建议或指示。

1 个答案:

答案 0 :(得分: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 |
+------+---------+------------+-------------+

对于ROLLUP

的敬意

你的查询非常大,这就是我选择再举一个例子的原因!