忽略CTE的日期列表

Bil*_*lla 5 sql sql-server sql-server-2008

CTE给出了以下结果

    Name            | StartDateTime           |  EndDateTime        
--------------------+-------------------------+------------------------
Hair Massage        | 2014-02-15 09:00:00.000 | 2014-02-15 10:00:00.000
Hair Massage        | 2014-02-15 10:00:00.000 | 2014-02-15 11:00:00.000
(X)Hair Massage     | 2014-02-23 09:00:00.000 | 2014-02-23 10:00:00.000
(X)Hair Cut         | 2014-02-20 12:15:00.000 | 2014-02-20 13:00:00.000
Hair Cut            | 2014-03-07 11:30:00.000 | 2014-03-07 12:15:00.000
Run Code Online (Sandbox Code Playgroud)

我也有假期

    Id       | StartDateTime      |  EndDateTime        
-------------+--------------------+-------------------
    1        | 20140223 00:00:00  | 20140224 23:59:00
Run Code Online (Sandbox Code Playgroud)

EventBooking

    EventId  | StartDateTime           |  EndDateTime        
-------------+-------------------------+------------------------
    1        | 2014-02-20 12:15:00.000 | 2014-02-20 13:00:00.000
Run Code Online (Sandbox Code Playgroud)

我想holidays and EventBooking从我的CTE中删除日期.我的意思是(X)从我的CTE中删除recods

RESULT=CTE- BookedSchedule-Holidays

with HoliDaysCte2 as
(
select StartdateTime,EndDateTime from Holidays
union all
select StartdateTime,EndDateTime from EventBooking
)

SELECT
    Name, 
    StartDateTime, 
    EndDateTime

FROM CTE WHERE not exists (select 1
                from HoliDaysCte2 h
                where cast(a.RepeatEventDate as DATETIME) between 
                   cast(h.startdatetime as DATETIME) 
                   and cast(h.enddatetime as DATETIME)
               )
Run Code Online (Sandbox Code Playgroud)

这是我的SQL FIDDLE DEMO

Sat*_*ath 4

好的,假设这是你的架构

CREATE TABLE dbo.StaffSchedule
(       ID INT IDENTITY(1, 1) NOT NULL,
        Name Varchar(50),       
        StartdateTime DATETIME2 NOT NULL,
        EndDateTime DATETIME2 NOT NULL
);

CREATE TABLE dbo.BookedSchedules
(       ID INT IDENTITY(1, 1) NOT NULL,
        StaffId INT,        
        StartdateTime DATETIME2 NOT NULL,
        EndDateTime DATETIME2 NOT NULL
);


CREATE TABLE dbo.Holidays
(       ID INT,
        StartdateTime DATETIME2 NOT NULL,
        EndDateTime DATETIME2 NOT NULL
);
INSERT dbo.StaffSchedule (Name, StartdateTime, EndDateTime)
VALUES 
    ('Hair Massage','2014-02-15 09:00:00.000','2014-02-15 10:00:00.000'),
    ('Hair Massage','2014-02-15 10:00:00.000','2014-02-15 11:00:00.000'),
    ('(X)Hair Massage','2014-02-23 09:00:00.000','2014-02-23 10:00:00.000'),
    ('(X)Hair Cut','2014-02-20 12:15:00.000','2014-02-20 13:00:00.000'),
    ('Hair Cut','2014-03-07 11:30:00.000','2014-03-07 12:15:00.000');


INSERT dbo.BookedSchedules (StaffId, StartdateTime, EndDateTime)
VALUES 
(1,'2014-02-20 12:15:00.000','2014-02-20 13:00:00.000');

INSERT dbo.Holidays (ID,StartdateTime, EndDateTime)
VALUES 
(1,'20140223 00:00:00','20140224 23:59:00');
Run Code Online (Sandbox Code Playgroud)

这能解决您的问题吗?

select * from StaffSchedule SS
where 
not exists(
select * from NonBookingSlots NBS
where (dateadd(MICROSECOND,1,ss.StartdateTime) 
    between nbs.StartdateTime and nbs.EndDateTime) 
        or (dateadd(MICROSECOND,-1,ss.EndDateTime) 
    between nbs.StartdateTime and nbs.EndDateTime))
Run Code Online (Sandbox Code Playgroud)