Reputation: 497
The Scenarion is DEMO001 system is booked from 10-Aug to 11-Aug by some user.
START_DATE END DATE SYSTEM
2016-08-10 2016-08-11 DEMO001
2016-09-05 2016-09-08 DEMO001
2016-08-08 2016-08-11 DEMO013
2016-08-16 2016-08-18 DEMO017
If Other user tries to book this system between these days . We have to check and pass 0 or 1.
I will get an input parameter
1) start_date as 2016-08-08 and 2016-08-15.
2) 2016-09-06 and 2016-09-07
3) 2016-08-08 and 2016-08-09
I have to write a PLSQL/SQL code to pass 0 because DEMO001 is falling between these start data and end date of the input else pass 1.
Kindly help.
Upvotes: 1
Views: 273
Reputation: 520938
Based on your requirements, you want to disallow a booking if either the start or end date falls in between a given record. You can just use a CASE
expression for this. I have hardcoded 2016-08-08
and 2016-08-15
as the start and end dates which you are using.
SELECT SYSTEM,
CASE WHEN (DATE '2016-09-06' < START_DATE AND DATE '2016-09-07' < START_DATE) OR
(DATE '2016-09-06' > END_DATE AND DATE '2016-09-07' > END_DATE)
THEN 1
ELSE 0
END AS allowBooking
FROM yourTable
Here is a link to a SQL Fiddle showing the query in action for the input range of 2016-08-08
to 2016-08-09
. It clearly shows that all bookings are possible except for DEMO013
. I used MySQL but the logic should not change in going from MySQL to Oracle or vice-versa.
Upvotes: 1
Reputation: 167832
Oracle Setup:
CREATE TABLE table_name ( START_DATE, END_DATE, SYSTEM ) AS
SELECT DATE '2016-08-10', DATE '2016-08-11', 'DEMO001' FROM DUAL UNION ALL
SELECT DATE '2016-09-05', DATE '2016-09-08', 'DEMO001' FROM DUAL UNION ALL
SELECT DATE '2016-08-08', DATE '2016-08-11', 'DEMO013' FROM DUAL UNION ALL
SELECT DATE '2016-08-16', DATE '2016-08-18', 'DEMO017' FROM DUAL;
Query:
With the bind variables :start_date
and :end_date
as the inputs:
SELECT system,
MIN(
CASE
WHEN :end_date < start_date OR :start_date > end_date
THEN 1
ELSE 0
END
) AS can_book
FROM table_name
GROUP BY system;
Output:
For the inputs :start_date = DATE '2016-08-08'
and :end_date = DATE '2016-08-15'
:
SYSTEM CAN_BOOK
------- ----------
DEMO001 0
DEMO013 0
DEMO017 1
For the inputs :start_date = DATE '2016-09-06'
and :end_date = DATE '2016-09-07'
:
SYSTEM CAN_BOOK
------- ----------
DEMO001 0
DEMO013 1
DEMO017 1
For the inputs :start_date = DATE '2016-08-08'
and :end_date = DATE '2016-08-09'
:
SYSTEM CAN_BOOK
------- ----------
DEMO001 1
DEMO013 0
DEMO017 1
Upvotes: 2