The table structure:
ID VersionID ShiftType Duration(Hr) FromDate ToDate
**********************************************************************
1 1 WORKING 12 20150702 20170528
2 1 WORKING 12 20150702 20170528
3 2 NOTWORKING 06 20170529 20170531
4 2 WORKING 06 20170529 20170531
5 2 NOTWORKING 06 20170529 20170531
6 2 WORKING 06 20170529 20170531
7 3 WORKING 08 20170601 0
8 3 NOTWORKING 08 20170601 0
9 3 WORKING 08 20170601 0
Here you can see I have 3 version's(1,2,3) of shift(like day-shifts for 10 days and night-shifts for next 20 days etc).
The present followed shift will not have the end time(Row 7,8 and 9).
For example from 20150702 to 20170528 I followed version 1 which is of total 24 hours(1 day) working with two shifts and from 20170529 to 20170531 followed version 2 with four shifts two working and two nonworking of total 24 hours(1 day).
Now if user provides date range I should get the shift followed on that date range.
Provided date range: From 20170528 To 20170614.
Expected result: All Rows.
Working with below query:
SELECT * FROM SHIFTS
WHERE (FFROMDATE >= 20170528 AND FFROMDATE <= 20170614) OR (FTODATE >= 20170528 AND FTODATE <= 20170614)
But doesn't work for: From 20170502 To 20170514.
Expected result: Rows 1,2(But none selected).
But doesn't work for: From 20170604 To 20170714.
Expected result: Rows 7,8,9(But none selected).
Thank you.