0

我有一张如下表:

StudentID CourseID    StartDate    EndDate
    1        1       12/21/2012    12/23/2012
    1        2       12/31/2012    1/2/2013
    2        1       12/10/2012    12/10/2012

如何获得如下输出(使用 CTE 或任何其他技术)

StudentID CourseID    Date
   1         1      12/21/2012
   1         1      12/22/2012
   1         1      12/23/2012
   1         2      12/31/2012
   1         2      01/01/2013
   1         2      01/02/2013
   2         1      12/10/2012
4

1 回答 1

2

您可以使用以下内容:

;with dates(studentid, courseid, date, enddate) as
(
  select studentid, courseid, startdate, enddate
  from yourtable
  union all
  select studentid, courseid, dateadd(day, 1, date), enddate
  from dates
  where dateadd(day, 1, date) <= enddate
)
select studentid, courseid, date
from dates
order by studentid, courseid;

请参阅带有演示的 SQL Fiddle

结果是:

| STUDENTID | COURSEID |       DATE |
-------------------------------------
|         1 |        1 | 12/21/2012 |
|         1 |        1 | 12/22/2012 |
|         1 |        1 | 12/23/2012 |
|         1 |        2 | 12/31/2012 |
|         1 |        2 | 01/01/2013 |
|         1 |        2 | 01/02/2013 |
|         2 |        1 | 12/10/2012 |
于 2013-01-04T13:19:42.950 回答