[英]SQL query for Calculating Total No. of Orders per Day?
Can anyone post a SQL query for Calculating Total No. of Orders per Day? 任何人都可以发布SQL查询来计算每日订单总数吗?
Here are the Columns along with their data in my Database. 以下是我的数据库中的列及其数据。
order_id order_placed_date order_total
- 1 12/30/2008 12:06:24 AM 2499.99
- 2 2/3/2009 1:57:17 AM 199.99
- 3 2/3/2009 1:58:27 AM 449.99
- 4 5/3/2009 1:58:48 AM 299.99
- 5 6/3/2009 2:00:31 AM 359.94
- 6 6/3/2009 2:01:47 AM 279.97
- 7 6/3/2009 2:02:31 AM 1359.94
- 9 7/1/2009 2:21:18 PM 5099.98
- 10 7/1/2009 2:21:36 PM 2621.97
- 11 7/2/2009 2:22:18 PM 2169.95
- 12 7/3/2009 2:23:29 PM 2249.95
- 13 7/4/2009 2:24:24 PM 5509.95
- 14 7/5/2009 12:15:17 AM 449.99
- 15 7/5/2009 12:18:08 AM 2299.99
- 16 7/5/2009 12:18:28 AM 3999.99
- 17 7/5/2009 12:18:45 AM 1939.99
- 18 7/5/2009 11:58:07 PM 39.99
- 19 7/6/2009 12:00:42 AM 1899.99
- 20 7/6/2009 12:01:00 AM 3999.99
- 21 7/7/2009 12:06:38 AM 199.99
- 22 7/7/2009 12:08:31 AM 1143.97
- 23 7/7/2009 12:09:13 AM 449.99
- 26 7/15/2009 1:30:03 PM 5469
- 27 7/15/2009 2:14:24 PM 329.97
- 28 7/15/2009 6:18:47 PM 5469
- 29 7/15/2009 10:17:36 PM 39.99
For eg there are 2 orders in the month of Febuary 2009 例如,2009年2月有2个订单
- 2 2/3/2009 1:57:17 AM 199.99
- 3 2/3/2009 1:58:27 AM 449.99
I need a sql query which would calculate and show the total amount per day. 我需要一个sql查询,它将计算并显示每天的总量。 So for 3rd of Feb 2009, the total would be 699.98
所以在2009年2月3日,总数将是699.98
I need to display Total Order Amount per day in a Chart 我需要在图表中显示每日总订单金额
If it would be easier to do it with PHP, do mention it as well. 如果用PHP做起来会更容易,那么也要提一下。
UPDATE: 更新:
I would like to clarify that I needed the Total Amount Per Day in the Present Month. 我想澄清一下,我需要当月的每日总金额。 I forgot to mention that in my initial question.
我在最初的问题中忘了提到这一点。
So i udpated Peter's query to get Total No. of Orders + Total Amount per Day in this Month. 所以我用Peter的查询来获得本月订单总数+每日总金额。
Please, let me know if it needs any correction or is there a better and shorter way of doing it. 请告诉我是否需要进行任何修正,或者是否有更好更短的方法。
SELECT date(order_placed_date), COUNT(order_id) AS num_orders, SUM(order_total) AS daily_total
FROM orders
WHERE order_placed_date>=date_sub(current_date, INTERVAL 31 DAY)
GROUP BY date(order_placed_date)
MySQL's date() function will return a DATEIME or TIMESTAMP value without any hour/minute/second info - which means you reduce the accuracy to the day of the value. MySQL的date()函数将返回DATEIME或TIMESTAMP值而没有任何小时/分钟/秒信息 - 这意味着您将精度降低到值的当天。
So all you need to do is group by that and then add your aggregate functions to the right columns. 因此,您需要做的就是将其分组,然后将聚合函数添加到正确的列中。
SELECT date(order_placed_date)
, COUNT(id) AS num_orders
, SUM(order_total) AS daily_total
FROM [Table]
GROUP BY date(order_placed_date)
SELECT date(order_placed_date)
, COUNT(id) AS num_orders
, SUM(order_total) AS daily_total
FROM orders
GROUP BY 1
Just copied Peter's answer but altered it so it will work. 刚刚复制了Peter的答案但改变了它以便它可以工作。 Plus shortcuted the group by.
加上这个小组被扼杀了。
A group by is your friend here. 一群人是你的朋友。 It can aggregate on grouped rows.
它可以在分组行上聚合。 An example query would be:
一个示例查询将是:
SELECT order_placed_date, SUM(order_total)
FROM orders
GROUP BY order_placed_date
Of course, in your example you'd probably want to extract just the day/month/year part using the DATE() function, and group by that. 当然,在您的示例中,您可能希望使用DATE()函数仅提取日/月/年部分,然后将其分组。
I don't know what it is in MySQL but in MSSQL it would be this, 我不知道它在MySQL中是什么,但在MSSQL中它就是这个,
select
datepart(yyyy, order_placed_date),
datepart(mm, order_placed_date),
datepart(dd, order_placed_date),
sum(order_total)
from orders
group by datepart(yyyy, order_placed_date), datepart(mm, order_placed_date), datepart (dd, order_placed_date)
select YEAR(datename(year,ORDER_DATE)) as YEARS,
count(ORDER_nO) as [NO(ORDER)]
from ProductOrder
group by YEAR(datename(year,ORDER_DATE))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.