While writing a SQL query I had to solve a problem I'd never dealt with before. It was trivial, but I cannot explain the solution without drawing lines on paper or making examples with actual numbers - and I don't like it: I want to give a rigorous answer and I want to learn about this subject.
The problem
- There are objects with
starts_on
andexpires_on
properties (SQL typeDATE
),starts_on
must be less or equal toexpires_on
. - We say the object is active at date D if
starts_on ≤ D ≤ expires_on
- We say the object is active in the range of dates R(range_start, range_end) if it's active in at least one date D,
range_start ≤ D ≤ range_end
- It turns out that the final where clause is
starts_on ≤ range_end AND expires_on ≥ range_start
How do mathematicians solve this problem? How can I explain where my SQL comes from without any fancy painting? I think the starting point may be the function:
active_at_date = f(starts_on, expires_on, target_date)
which returns:
- TRUE, if
starts_on ≤ target_date ≤ expires_on
- FALSE otherwise
But then, how could I go on?
Hope my question is not offtopic here. I worded it as best as I can, but it's the first attempt on this stackexchange site (and my math knowledge is very limited in this context, I know)
ends_on
meansexpires_on
? Because you don't explain whatends_on
means. $\endgroup$