James Z is right on the money, but to expand on what it does in the answer you were referencing:
In the code that is pulled from, that section is used to start numbers table for a stacked cte
. The numbers themselves don't matter, but I like them like that. They could all be 1, or 0, it would not change how it is used in this instance.
Basically we have 10 rows, and then we are going to cross join it to self N
number of times to increase the row count until as many or more than we need. In the cross join
I alias n
with the resulting amount of rows deka
is 10, hecto
is 100, kilo
is 1,000, et cetera.
Here is a similar query outside of the function that you were referencing:
declare @fromdate date = '20000101';
declare @years int = 30;
;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 [Date]
from dates;
The stacked cte
is very efficient for generating or simulating a numbers or dates table, though using an actual numbers
or calendar
table will perform better as the scale increases.
Check these out for related benchmarks:
In hist articles, Aaron Bertrand creates a stacked cte
using
;WITH e1(n) AS
(
SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL
SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL
SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1
),
e2(n) AS (SELECT 1 FROM e1 CROSS JOIN e1 AS b),
....