Я пытаюсь создать таблицу расписаний, используя Calendar.Date
для генерации пустых «ячеек».
Запрос:
select c.Date, d.DriverId, d.RouteId, d.StartTime
FROM Calendar c
left outer join
(
select 'ABC' DriverId, 101 RouteId, cast('2019-08-26 08:30:00.000' as datetime) StartTime
union all
select 'ABC' DriverId, 101 RouteId, cast('2019-08-29 08:30:00.000' as datetime) StartTime
union all
select 'DEF' DriverId, 201 RouteId, cast('2019-08-27 11:30:00.000' as datetime) StartTime
) d ON d.StartTime >= c.Date AND d.StartTime < DATEADD(DAY, 1, c.Date)
WHERE 1=1
and c.date between '08/26/19' and '08/29/19'
Результаты:
Date,DriverId,RouteId,StartTime
2019-08-26 00:00:00.000,ABC,101,2019-08-26 08:30:00.000
2019-08-27 00:00:00.000,DEF,201,2019-08-27 11:30:00.000
2019-08-28 00:00:00.000,,,
2019-08-29 00:00:00.000,ABC,101,2019-08-29 08:30:00.000
Желаемый:
Date,DriverId,RouteId,StartTime
2019-08-26 00:00:00.000,ABC,101,2019-08-26 08:30:00.000
2019-08-26 00:00:00.000,DEF,,
2019-08-27 00:00:00.000,ABC,,
2019-08-27 00:00:00.000,DEF,201,2019-08-27 11:30:00.000
2019-08-28 00:00:00.000,ABC,,
2019-08-28 00:00:00.000,DEF,,
2019-08-29 00:00:00.000,ABC,101,2019-08-29 08:30:00.000
2019-08-29 00:00:00.000,DEF,,
A CROSS JOIN
:
select c.Date, d.DriverId, d.RouteId, d.StartTime
FROM Calendar c
cross join
(
select 'ABC' DriverId, 101 RouteId, cast('2019-08-26 08:30:00.000' as datetime) StartTime
union all
select 'ABC' DriverId, 101 RouteId, cast('2019-08-29 08:30:00.000' as datetime) StartTime
union all
select 'DEF' DriverId, 201 RouteId, cast('2019-08-27 11:30:00.000' as datetime) StartTime
) d
WHERE 1=1
and c.date between '08/26/19' and '08/29/19'
не дает желаемого результата:
Date,DriverId,RouteId,StartTime
2019-08-26 00:00:00.000,ABC,101,2019-08-26 08:30:00.000
2019-08-26 00:00:00.000,ABC,101,2019-08-29 08:30:00.000
2019-08-26 00:00:00.000,DEF,201,2019-08-27 11:30:00.000
2019-08-27 00:00:00.000,ABC,101,2019-08-26 08:30:00.000
2019-08-27 00:00:00.000,ABC,101,2019-08-29 08:30:00.000
2019-08-27 00:00:00.000,DEF,201,2019-08-27 11:30:00.000
2019-08-28 00:00:00.000,ABC,101,2019-08-26 08:30:00.000
2019-08-28 00:00:00.000,ABC,101,2019-08-29 08:30:00.000
2019-08-28 00:00:00.000,DEF,201,2019-08-27 11:30:00.000
2019-08-29 00:00:00.000,ABC,101,2019-08-26 08:30:00.000
2019-08-29 00:00:00.000,ABC,101,2019-08-29 08:30:00.000
2019-08-29 00:00:00.000,DEF,201,2019-08-27 11:30:00.000
Есть ли способдостичь результатов, не прибегая к T/SQL
?