关于SQL group by week的一些问题
我在按周编写 SQL 分组时遇到一些问题。
我有一个名为 order 的 MySQL 表。
在这个实体中,有几个属性,称为“order_id”,“order_date”,“amount”等。
我想制作一个表格来显示过去7天订单销售额的统计数据。
我想首先我应该得到今天的价值。
由于我使用Java Server Page,代码如下:
Calendar cal = Calendar.getInstance();
int day = cal.get(Calendar.DATE);
int Month = cal.get(Calendar.MONTH) + 1;
int year = cal.get(Calendar.YEAR);
String today = year + "-" + Month + "-" + day;
然后,我需要使用group by语句来计算过去7天的总销售额的SUM。
像这样:
ResultSet rs=statement.executeQuery("select order_date, SUM(amount) " +
"from `testing`.`order` GROUP BY order_date");
我这里有问题。在我的SQL中,将显示所有order_date。
如何修改此 SQL 以便仅显示过去 7 天的订单销售额?
除此之外,我发现我原来的 SQL 存在问题。
也就是说,如果当天没有销售,则不会显示任何结果。
当然,我知道 ResultSet 不允许在我的 SQL 中返回空值。
我只是想知道我是否需要过去7笔订单的销售,即使金额是0美元,
我可以有其他方法来显示0吗?
如果您有想法,请给我建议。
谢谢。
I have some problems when coding SQL group by week.
I have a MySQL table named order.
In this entity, there are several attributes, called 'order_id', 'order_date', 'amount', etc.
I want to make a table to show the statistics of past 7 days order sales amount.
I think first I should get the today value.
Since I use Java Server Page, the code like this:
Calendar cal = Calendar.getInstance();
int day = cal.get(Calendar.DATE);
int Month = cal.get(Calendar.MONTH) + 1;
int year = cal.get(Calendar.YEAR);
String today = year + "-" + Month + "-" + day;
then, I need to use group by statement to calculate the SUM of past 7 day total sales amount.
like this:
ResultSet rs=statement.executeQuery("select order_date, SUM(amount) " +
"from `testing`.`order` GROUP BY order_date");
I have problem here. In my SQL, all order_date will be displayed.
How can I modify this SQL so that only display past seven days order sale amount?
Besides that, I discover a problem in my original SQL.
That is, if there is no sales on that day, no results would be displayed.
OF course, I know the ResultSet does not allow return null values in my SQL.
I just want to know if I need the past 7 order sales even the amount is 0 dollars,
Can I have other methods to show the 0?
Please kindly give me advices if you have idea.
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
通常会使用脚本或存储过程创建包含所有日期的日历表。
但是,如果您愿意,可以使用单个查询创建一个包含几个日期(在您的情况下是上周的日期)的表。
这是一个例子:
希望有帮助。问候。
Usually it occurs to create with a script or with a stored procedure a calendar table with all dates.
However if you prefer you can create a table with few dates (in your case dates of last week) with a single query.
This is an example:
Hope that it helps. Regards.
回答您的问题“如何修改此 SQL,以便仅显示过去 7 天的订单销售额?”
通过添加 where 子句来修改 SQL 语句:
Where order_date >= @date_7days_ago
可以在语句之前设置此 @date_7days_ago 日期变量的值:
())
Select @date_7days_ago = dateadd(dd,-7, getdate 查询的 where 子句将仅返回订单日期在过去 7 天内的记录。
希望这有帮助。
To answer your question "How can I modify this SQL so that only display past seven days order sale amount?"
Modify the SQL statement by adding a where clause to it:
Where order_date >= @date_7days_ago
The value for this @date_7days_ago date variable can be set before your statement:
Select @date_7days_ago = dateadd(dd,-7,getdate())
Adding that where clause to your query will return only those records which order date is in the last seven days.
Hope this helps.
您可以尝试使用这个:
这将为您提供过去 7 天内的订单数,如果没有则为 0。
You can try using this:
This will get you the number of orders made in the last 7 days, and 0 if there were none.