Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 嗯!! <-------I want to book this - How many seats are available?-------> <---Booking 1: 10 seats---> <--_Sql_Sql Server 2008_Tsql - Fatal编程技术网

Sql 嗯!! <-------I want to book this - How many seats are available?-------> <---Booking 1: 10 seats---> <--

Sql 嗯!! <-------I want to book this - How many seats are available?-------> <---Booking 1: 10 seats---> <--,sql,sql-server-2008,tsql,Sql,Sql Server 2008,Tsql,嗯!! <-------I want to book this - How many seats are available?-------> <---Booking 1: 10 seats---> <---Booking 2: 10 seats---> <---Booking 3: 10 seats--->

嗯!!
<-------I want to book this - How many seats are available?------->

   <---Booking 1: 10 seats--->       <---Booking 2: 10 seats--->
                       <---Booking 3: 10 seats--->
                                     <---Booking 4: 10 seats--->
create table Room
(
    id int identity(1, 1) primary key clustered,
    capacity int not null
)
go
create table RoomBooking
(
    id int identity(1, 1) primary key clustered,
    room int constraint FK_RoomBooking_Room foreign key references Room(id),
    start_time datetime,
    end_time datetime,
    capacity_taken int
)
go

insert Room(capacity)
    select 100 union all
    select 200

insert RoomBooking(room, start_time, end_time, capacity_taken)
    select 1, '2012-02-29 10:00', '2012-02-29 12:00', 10 union all
    select 1, '2012-02-29 11:00', '2012-02-29 15:00', 10 union all
    select 1, '2012-02-29 14:00', '2012-02-29 16:00', 10 union all
    select 2, '2012-02-29 14:00', '2012-02-29 16:00', 10 union all
    select 2, '2012-02-29 14:00', '2012-02-29 16:00', 10 union all
    select 2, '2012-02-29 14:00', '2012-02-29 16:00', 10 union all
    select 2, '2012-02-29 13:00', '2012-02-29 15:00', 10 union all
    select 2, '2012-02-29 15:00', '2012-02-29 17:00', 10 union all
    select 2, '2012-02-29 17:00', '2012-02-29 19:00', 10 union all
    select 1, '2012-02-29 14:00', '2012-02-29 16:00', 10

go

declare @roomid int = 1
declare @check_period_start datetime = '2012-02-29 13:00'
declare @check_period_end datetime = '2012-02-29 15:00'

select
    r.id, r.capacity - maxtaken.max_capacity_taken as remaining_capacity
from
    Room r
    join
        (
            select
                id, MAX(sum_capacity_taken) max_capacity_taken
            from
                (
                    select
                        r.id, SUM(rb2.capacity_taken) + min(rb1.capacity_taken) sum_capacity_taken
                    from
                        Room r
                        join RoomBooking rb1 on rb1.room = r.id
                        left join RoomBooking rb2
                            on rb1.room = rb2.room
                               and rb1.id <> rb2.id
                               and 
                               (
                                   (rb2.start_time <= rb1.start_time and rb2.end_time >= rb1.start_time)
                                   or (rb2.start_time <= rb1.end_time and rb2.end_time >= rb1.end_time)
                               )
                        where
                            rb1.end_time >= @check_period_start
                            and rb1.start_time <= @check_period_end
                            and rb2.end_time >= @check_period_start
                            and rb2.start_time <= @check_period_end
                        group by
                            r.id, rb1.id
                ) sct
                group by id
        ) maxtaken on maxtaken.id = r.id
where
    r.id = @roomid
;with cte as 
(select id, capacity, @start_time time_point
 from room where id = @room
 union all
 select id, capacity, dateadd(mi, 1, time_point) time_point
 from cte where dateadd(mi, 1, time_point) < @end_time)
select min(capacity_left) max_available from
(select c.time_point, max(c.capacity) - sum(b.capacity_taken) capacity_left
 from cte c
 join RoomBooking b
   on c.id = b.room and
      c.time_point >= b.start and
      c.time_point < b.end
 group by c.time_point) sq
; with  hours as
        (
        select  @startdt as dt
        union all
        select  dateadd(hour, 1, dt)
        from    Hours
        where   dateadd(hour, 1, dt) < @enddt
        )
,       hour_capacity as
        (
        select  h.dt
        ,       r.capacity
        ,       sum(b.seats) as seats_used
        from    @room r
        cross join
                hours h
        join    @booking b
        on      b.roomid = r.id
                and h.dt between b.startdt and b.enddt
        where   r.id = @roomid
        group by
                h.dt
        ,       r.capacity
        )
select  capacity - max(seats_used)
from    hour_capacity
group by
        capacity