对每行的日期范围进行分组
我正在尝试输出每日、每周、每月和每年的订单数量以及这些时间范围内这些订单的总金额。目前我想出了以下内容。它很长,但它只是同一份声明的复制和粘贴工作,只是稍微修改为按年、月、周和日进行分组。
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
哪个是理想的输出,提供可以相应输出的年、月、周、日计数和交易金额。但这仅适用于一种类型的订单。在订单表中,有两种类型的订单:在线订单和终端订单,理想情况下,我希望在同一查询中输出这两种类型,而不是多次查询数据库。
我有两个问题。首先,是否有更简单的方法可以达到相同的结果?我不想让它变得过于复杂,因为导致第二个问题,因为有些订单不同,如果我简单地复制和联合我已有的代码和添加的',我希望结果显示在线订单和终端订单WHERE Terminal = 1' 由于并集,结果是相同的。有没有办法在同一个查询中输出两组输出?
我的目标可能过于复杂化,但提前感谢您的任何建议或指示。
I'm trying to output the daily, weekly, monthly and years count of orders and the sum amount of those orders between those time ranges. At the moment I have come up with the following. Its long, but its just a copy and paste job of the same statement just amended slightly to group by the year, month, week and day.
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)
Which outputs
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
Which is a ideal output providing the year, month, week, daily count and transactions sum which can be outputted accordingly. However that's for only one type of order. In the orders table there's two types of orders, online and terminal and ideally I'd like to output both types in the same query instead of query the database multiple times.
I have two questions which I'm having issues with. Firstly, is there a simpler way to achieve the same result? I don't wish to over complicate it because leading onto the second question, because some orders differ and I'd like the result to show both online orders and terminal orders if I simply copy and UNION the code I already have with the added 'WHERE Terminal = 1' because of the union the results are the same. Is there a way to output two sets of outputs in the same query?
I'm probably over complicating what I'm aiming for, but thank you in advance for any advice or pointers.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你可以像这个例子一样尝试使用
GROUP BY WITH ROLLUP
结果将是
For Rerence with ROLLUP
您的查询非常大,这就是为什么我选择再举一个例子!
can you try like this example by using
GROUP BY WITH ROLLUP
Result will be
For Rerence with ROLLUP
Your query is pretty big that's why i choose to give another example!