SQL速度:返回日期范围内的每个日期,并为每个日期返回count()
我的目标是返回日期范围内的每个日期,并计数()每个日期的所有记录SQL速度:返回日期范围内的每个日期,并为每个日期返回count(),sql,sql-server,Sql,Sql Server,我的目标是返回日期范围内的每个日期,并计数()每个日期的所有记录 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-
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 |
---------------------
为了实现这一点,我使用此查询仅选择日期,并在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)
问题是,当我将这两个查询合并到一个SQL语句中时左连接执行需要20秒(!!!)。我尝试了一个“咯咯笑”的子查询,但在13秒时效果并没有太好:
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)
如何有效地加入结果数据集?
提前感谢您的时间。使用递归cte是生成日期范围最糟糕的方法之一。与使用递归cte相比,使用堆叠cte可根据需要生成日期范围
如果要跨多行或长时间使用它,或者要多次运行此类操作,最好只创建一个日期
或日历
表
内存只有152kb,表中可以有30年的日期,可以这样使用:
/* dates table */
declare @fromdate date = '20000101';
declare @years int = 30;
/* 30 years, 19 used data pages ~152kb in memory, ~264kb on disk */
;with n as (select n from (values(0),(1),(2),(3),(4),(5),(6),(7),(8),(9)) t(n))
select top (datediff(day, @fromdate,dateadd(year,@years,@fromdate)))
[Date]=convert(date,dateadd(day,row_number() over(order by (select 1))-1,@fromdate))
into dbo.Dates
from n as deka cross join n as hecto cross join n as kilo
cross join n as tenK cross join n as hundredK
order by [Date];
create unique clustered index ix_dbo_Dates_date
on dbo.Dates([Date]);
并这样查询:
select
d.[Date]
, OrderCount = count(o.OrderID)
from dates d
left join orders o
on convert(date,o.OrderDate) = d.[Date]
group by d.[Date]
order by d.[Date] desc
编号和日历表参考:
如果您确实不想要日历表,可以使用堆叠的cte部分:
declare @fromdate date = '20160101';
declare @years int = 1;
;with n as (select n from (values(0),(1),(2),(3),(4),(5),(6),(7),(8),(9)) t(n))
, dates as (
select top (datediff(day, @fromdate,dateadd(year,@years,@fromdate)))
[Date]=convert(date,dateadd(day,row_number() over(order by (select 1))-1,@fromdate))
from n as deka cross join n as hecto cross join n as kilo
/* cross join n as tenK cross join n as hundredK */
order by [Date]
)
select
d.[Date]
, OrderCount = count(o.OrderID)
from dates d
left join orders o
on convert(date,o.OrderDate) = d.[Date]
group by d.[Date]
order by d.[Date] desc
使用索引:尝试使用数字表而不是递归cte。我用过