T-SQL - 向 table 所有行添加更多日期值

T-SQL - adding more date values to a table all rows

我有一个 table,其中包含许多 ID 作为随机数,我想创建另一个 table,其中包含所有 ID,另一列的日期从一年的第一天到具体日期。

例如:我的实际 table 看起来像:

ID  
101  
431  
566

如果我的具体结束日期是 2020-01-03,我想要一个 table,看起来像:

ID Date  
101 2020-01-01  
101 2020-01-02  
101 2020-01-03  
431 2020-01-01  
431 2020-01-02  
431 2020-01-03  
566 2020-01-01  
566 2020-01-02  
566 2020-01-03

你能帮我解决我的问题吗?提前致谢!

您可以使用递归 CTE 定义日期,然后 cross join:

with dates as (
      select convert(date, '2020-01-01') as dte
      union all
      select dateadd(day, 1, dte)
      from dates
      where dte < @enddate
     )
select t.id, dates.date
from t cross join
     dates
option (maxrecursion 0);