I have a table with the columns ID
, start_time
, and end_time
.
I would like to extract all the records that are active during a date range.
ID | start_time | end_time
----|------------|------------
1 | 2019-07-22 | 2019-07-25
2 | 2019-07-20 | 2019-07-25
3 | 2019-07-20 | 2019-07-21
4 | 2019-07-20 | 2019-07-25
5 | 2019-07-22 | 2019-07-22
6 | 2019-07-25 | 2019-07-25
7 | 2019-07-26 | 2019-07-27
Example Query parameters
- start: 2019-07-22
- end: 2019-07-25
Expected results (5 rows):
ID | start_time | end_time
----|------------|------------
1 | 2019-07-22 | 2019-07-25
2 | 2019-07-20 | 2019-07-25
4 | 2019-07-20 | 2019-07-25
5 | 2019-07-22 | 2019-07-22
6 | 2019-07-25 | 2019-07-25
the row containing ID 7 is omitted, because it is not active in the specified date range
I want to include the records with start_time
2019-07-20 because they are actually active on July 22.
If I use the following:
where start_time >= '2019-07-22' and end_time <= '2019-07-25'
…then I am excluding the records starting on July 20.
How can I include those records?
Best Answer
So if you're trying to find all active/valid records in a time frame, and you are using intervals, then you must use the following criteria:
This is working on the assumption that a record is still effective as of the end date, as indicated in your example.
So your query becomes:
A simple truth table helps verify:
Other notes: Those are dates, not times. Column should be Start_Date/End_Date or Start_Datetime/End_Datetime if you are include hours/minutes/seconds (you can abbreviate with Dt/Dtm if you wish).
From comment:
So here we need to be careful with what question is actually be asked, and what answer the query is providing.
The statement
WHERE end_time >= '2019-07-22 00:00:00'
is the same as saying: any records that end OR start on/after2019-07-22 00:00:00
.The inverse of that statement would be any records that start AND end before
2019-07-22 00:00:00
, which isWHERE end_time < '2019-07-22 00:00:00'
.So if your question is actually "Which records end or start on/before
2019-07-22 00:00:00
the condition isWHERE start_time <= 2019-07-22 00:00:00