I'm looking for a function for my specific problem.
I have a system rent PC. Every PC model is available in a certain quantity, for example ACER XY = 7 units. TOSHIBA ZZ = 5 units...
User can select a range date (with time) and a (or more than 1) PC MODEL for the rent in this range.
For example from 2017-12-01 T 13:10.....to.......2017-12-05 T 21:00....with ACER XY
(2 units for rental)
My function as to search inside database if in this range there are some rental entry with this PC MODEL and return true or false for submitting form. (In this example so is accepted up to 5 entries...cause 7 total units minus 2 to rent = 5).
I don't know how to approach.
For example if in database i've already 2 sequentially events + 1 overlap event like:
2017-12-02 T 13:10 to 2017-12-05 T 21:00 with ACER XY quantity 1 customer AA
2017-12-05 T 21:00 to 2017-12-07 T 11:00 with ACER XY quantity 3 customer BB
2017-12-07 T 09:00 to 2017-12-09 T 08:00 with ACER XY quantity 3 customer CC
..................
If i try to insert 2017-12-01 T 13:10.....to.......2017-12-05 T 21:00....with ACER XY
(2 units for rental)
I need to return TRUE
If i try with 2017-12-01 T 13:10.....to.......2017-12-05 T 21:10....with ACER XY
(5 units for rental)
I need to return FALSE
If i try with 2017-12-01 T 13:10.....to.......2017-12-07 T 08:59....with ACER XY
(2 units for rental)
I need to return TRUE
If i try with 2017-12-01 T 13:10.....to.......2017-12-07 T 09:01....with ACER XY
(2 units for rental)
I need to return FALSE
I hope the example are good to understand?
My tables (simplified):
Table RentalTimeline:
ID (INT), start (DATETIME), end (DATETIME), pc_id (INT), quantityForRent (INT), customer_ID (INT)
Table PC:
ID (INT), ModelName (varchar), QuantityAvailableTotal (INT)
UPDATE: @Juan Carlos Oropeza solution i think doesn't work cause if I this situation: 7 TOTAL PC ACER AVAILABLE
and these 3 events already in DB (i remove times to simplify):
2017-12-02 to 2017-12-15 with ACER quantity 1 customer AA
2017-12-05 to 2017-12-06 with ACER quantity 3 customer BB
2017-12-07 to 2017-12-09 with ACER quantity 3 customer CC
and I try to insert an event:
2017-12-01 to 2017-12-20 with ACER quantity 1
With @Juan Carlos Oropeza solution I'll get FALSE cause with my future new entry I'll cover all 3 events already inserted...and the SUM is equal to 7 (my max PC available), but 2 events are sequentially then i need to return TRUE in this case (cause from 12-01 to 12-01 I'll have 1 PC (the new inserted), from 12-02 to 12-04 I'll have 2 PC (1+1new), from 12-05 to 12-06 I'll have 5 pc (1+3+1new), from 12-07 to 12-09 I'll have 5 pc (1+3+1new), from 12-10 to 12-15 I'll have 2 pc (1+1new) and from 12-16 to 12-20 I'll have 1 pc (1new))