【发布时间】:2017-04-03 23:08:51
【问题描述】:
我的目标是返回一个日期范围内的每个日期,并计算每个日期的所有记录。
MyTable
-------------------------------
| OrderId | DateFinalized |
-------------------------------
| 51 | 2016-1-3 12:50:34 |
| 55 | 2016-1-4 10:01:56 |
| 73 | 2016-1-4 11:52:02 |
| 93 | 2016-1-6 01:35:16 |
| 104 | 2016-1-6 02:40:47 |
-------------------------------
挑战是也包括没有订单的日期。使用上面的MyTable,如果日期范围在2016-1-1 和2016-1-6 之间,则所需的输出为:
---------------------
| MyDate | Orders |
---------------------
| 2016-1-1 | 0 |
| 2016-1-2 | 0 |
| 2016-1-3 | 1 |
| 2016-1-4 | 2 |
| 2016-1-5 | 0 |
| 2016-1-6 | 2 |
---------------------
为此,我使用此查询来选择 dates only,并在 1 秒内执行:
declare @startdate datetime = '1/1/2016';
declare @enddate datetime = '1/1/2017';
with [dates] as (
select convert(date, @startdate) as [date]
union all
select dateadd(day, 1, [date])
from [dates]
where [date] < @enddate
)
select
[date]
from [dates]
where [date] between @startdate and @enddate
order by [date] desc
option (maxrecursion 0)
当我选择按日期分组的订单计数时,如下所示,它也只需要 1 秒:
declare @startdate datetime = '2/1/2016';
declare @enddate datetime = '1/1/2017';
select
convert(date,DATEADD(dd, DATEDIFF(dd, 0, datefinalized), 0)) as Dates,
count(OrderID) as OrderCount
from orders
where datefinalized between @startdate and @enddate
GROUP BY DATEADD(dd, DATEDIFF(dd, 0, datefinalized), 0)
order by DATEADD(dd, DATEDIFF(dd, 0, datefinalized), 0) desc
问题是当我将这两个查询组合在一个 SQL 语句中时。 LEFT JOIN 需要 20 秒(!!!) 来执行。我尝试了一个用于咯咯笑的子查询,但在 13 秒时并没有好多少:
如何有效地加入生成的数据集?
提前感谢您的宝贵时间。
【问题讨论】:
-
尝试使用数字表而不是递归 cte。我用过rextester to check it but couldn't reproduce the problem.
标签: sql sql-server