-5

I have a table like this

employee       | wrok_from     |until
-------------------------------------------
Emp1           | 2017-01-01    |2017-01-15
Emp2           | 2017-01-10    |2017-01-25
Emp3           | 2017-01-20    |2017-02-10
Emp4           | 2017-01-30    |2017-02-20

I want to show every employee that work between two dates. exp: when I select date between 2017-01-23 and 2017-02-20 it will show me :

Emp2           | 2017-01-10    |2017-01-25
Emp3           | 2017-01-20    |2017-02-10
Emp4           | 2017-01-30    |2017-02-20

when I select date between 2017-01-10 and 2017-02-01 it will show me :

Emp1           | 2017-01-01    |2017-01-15
Emp2           | 2017-01-10    |2017-01-25
Emp3           | 2017-01-20    |2017-02-10
Emp4           | 2017-01-30    |2017-02-20

1 Answers1

0

I think this should work. I used :start and :end as placeholders for your input values.

The query selects all employees with their working dates when the working date is at least partially part of your input range.

Visualization (I: input range, y: selected, n: not selected):

--------IIIIIIIIIIIIIIIIIIII----------
--nnn-yyyy---yyyy---------yyyyy----nn-

Query:

SELECT * 
FROM workdates
WHERE     
    work_from < :end AND
    until > :start
Imanuel
  • 3,596
  • 4
  • 24
  • 46
  • 1
    It's really quite simple: Event A can be said to overlap Event B if Event A *starts* before Event B *ends*, and *ends* after Event B *starts*. – Strawberry Aug 07 '17 at 09:57
  • @Strawberry That's correct and much easier. Thank you for the hint, I changed the answer. – Imanuel Aug 07 '17 at 10:00