我有一个包含以下字段的表
ReservationID int
ReservationDateFrom datetime
ReservationDateTo datetime
ReservationRoom int
ReservationPurpose varchar
ReservationDetailsID int
我想创建一个sp,用于选择可用记录和不可用记录(如果至少30分钟)给定日期(必填),房间(可选),TimeFrom(可选),TimeTo(如果TimeFrom不为空则需要) 示例表
ReservationID ReservationDateFrom ReservationDateTo ReservationRoom ReservationPurpose ReservationDetailsID
3002 2017-01-02 00:00:00.000 2017-01-02 02:00:00.000 14 qweqwe 3002
3003 2017-01-02 04:00:00.000 2017-01-02 05:00:00.000 14 qweqwe 3002
3004 2017-01-02 06:00:00.000 2017-01-02 08:00:00.000 14 eqweq 3002
3005 2017-01-02 08:30:00.000 2017-01-02 09:30:00.000 14 iuyiyi 3002
3006 2017-01-02 09:50:00.000 2017-01-03 11:00:00.000 14 qweqwe 3003
要选择的结果(显示不可用且可用的房间和范围)
ReservationID ReservationDateFrom ReservationDateTo ReservationRoom ReservationPurpose ReservationDetailsID
3002 2017-01-02 00:00:00.000 2017-01-02 02:00:00.000 14 qweqwe 3002
3003 2017-01-02 04:00:00.000 2017-01-02 05:00:00.000 14 qweqwe 3002
3004 2017-01-02 06:00:00.000 2017-01-02 08:00:00.000 14 eqweq 3002
3005 2017-01-02 08:30:00.000 2017-01-02 09:30:00.000 14 iuyiyi 3002
3006 2017-01-02 09:50:00.000 2017-01-03 11:00:00.000 14 qweqwe 3003
2017-01-02 02:00:00.000 2017-01-02 04:00:00.000 14
2017-01-02 05:00:00.000 2017-01-02 06:00:00.000 14
2017-01-02 08:00:00.000 2017-01-02 08:30:00.000 14
2017-01-02 11:00:00.000 2017-01-03 24:00:00.000 14
答案 0 :(得分:0)
您可以添加"无保留"使用lead()
(或lag()
)的记录。基本思路是:
select ReservationID, ReservationDateFrom, ReservationDateTo, ReservationRoom
from reservations r
where . . .
union all
select NULL, ReservationDateTo,
lead(ReservationDateFrom) over (partition by ReservationRoom),
ReservationRoom
from reservations r
where . . .;
这并不能准确生成您指定的输出。但我不清楚某些逻辑来自何处,尤其是最后一行。