SQL按天分组,显示每天的订单
我有一个 SQL 2005 表,我们称之为“订单”,格式为:
OrderID, OrderDate, OrderAmount
1, 25/11/2008, 10
2, 25/11/2008, 2
3, 30/1002008, 5
然后我需要生成一个报告表,显示过去 7 天内每天的订购金额:
Day, OrderCount, OrderAmount
25/11/2008, 2, 12
26/11/2008, 0, 0
27/11/2008, 0, 0
28/11/2008, 0, 0
29/11/2008, 0, 0
30/11/2008, 1, 5
通常会生成以下内容的 SQL 查询:
select count(*), sum(OrderAmount)
from Orders
where OrderDate>getdate()-7
group by datepart(day,OrderDate)
有问题它将跳过没有订单的日子:
Day, OrderCount, OrderAmount
25/11/2008, 2, 12
30/11/2008, 1, 5
通常我会使用计数表和针对那里的行的外连接来修复此问题,但我确实正在为此寻找更简单或更有效的解决方案。 这似乎是报告查询的常见要求,应该已经有一些优雅的解决方案可用。
那么: 1. 不使用统计表,通过简单的查询可以得到这个结果吗?
2. 如果不是,我们可以(可靠地)即时创建这个统计表(我可以使用 CTE 创建一个统计表,但递归堆栈将我限制为 100 行)?
I have an SQL 2005 table, let's call it Orders, in the format:
OrderID, OrderDate, OrderAmount
1, 25/11/2008, 10
2, 25/11/2008, 2
3, 30/1002008, 5
Then I need to produce a report table showing the ordered amount on each day in the last 7 days:
Day, OrderCount, OrderAmount
25/11/2008, 2, 12
26/11/2008, 0, 0
27/11/2008, 0, 0
28/11/2008, 0, 0
29/11/2008, 0, 0
30/11/2008, 1, 5
The SQL query that would normally produce this:
select count(*), sum(OrderAmount)
from Orders
where OrderDate>getdate()-7
group by datepart(day,OrderDate)
Has a problem in that it will skip the days where there are no orders:
Day, OrderCount, OrderAmount
25/11/2008, 2, 12
30/11/2008, 1, 5
Normally I would fix this using a tally table and outer join against rows there, but I'm really looking for a simpler or more efficient solution for this. It seems like such a common requirement for a report query that some elegant solution should be available for this already.
So: 1. Can this result be obtain from a simple query without using tally tables?
and 2. If no, can we create this tally table (reliably) on the fly (I can create a tally table using CTE but recursion stack limits me to 100 rows)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
SQL 不会“跳过”日期...因为查询是针对表中实际存在的数据运行的。 因此,如果表中没有 1 月 14 日的日期,那么为什么 SQL 会向您显示结果:)
您需要做的是创建一个临时表,然后加入到它。
就这样吧!
SQL isn't "skipping" dates... because queries run against data that is actually in the table. So, if you don't have a DATE in the table for January 14th, then why would SQL show you a result :)
What you need to do is make a temp table, and JOIN to it.
There you go!
我遇到了同样的问题,这就是我解决它的方法:
输出是:
I had the same problem and this is how I solved it:
The ouput is:
根据 SQL Server 处理临时表的方式,您可以或多或少地轻松安排创建临时表并用您感兴趣的 7 个(或者是 8 个?)日期填充它。然后您可以将其用作统计表。 据我所知,没有更干净的方法; 您只能选择表中存在的数据或可以从表或表组中存在的数据派生的数据。 如果订单表中未显示某些日期,则您无法从订单表中选择这些日期。
Depending on how SQL Server handles temporary tables, you can more or less easily arrange to create a temporary table and populate it with the 7 (or was that 8?) dates you are interested in. You can then use that as your tally table. There isn't a cleaner way that I know of; you can only select data that exists in a table or that can be derived from data that exists in a table or set of tables. If there are dates not represented in the Orders table, you can't select those dates from the Orders table.
只需输入此代码并致电 SP
这样
谢谢
*Suvabrata Roy
ICRA在线有限公司
加尔各答*
Just put this Code and call the SP
in This way
Thanks
*Suvabrata Roy
ICRA Online Ltd.
Kolkata*
如果您想查看值零,请输入以下查询:
If you want to see value zero than put the following query:
由于您也希望在其他查询中经常使用此日期表,因此我建议您将其设为永久表并创建一个作业来每年添加一次新年日期。
Since you will want to use this date table frequently in other queries as well, I suggest you make it a permanent table and create a job to add the new year's dates once a year.