A
A
A_Modestov2017-06-19 10:44:36
PostgreSQL
A_Modestov, 2017-06-19 10:44:36

How to select recurring events from the database?

Good day to all. The task is this:
There is an Event model with fields start_date, end_date, repeat. An event can be repeated every day, month, year (information about this is stored in the repeat field). Can you please tell me how to formulate a query to the database in order to receive events from an interval, including those created not in this interval, but providing for repeatability?
Example: Event created on 05/01/2017, repeats every month. I send a request for the dates 06/1/2017-06/30/2017, as a result, I should receive this event in the selection, as a repeating every month.
Thank you for your help.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Melkij, 2017-06-19
@melkij

If on the forehead:

select ... from events where exists (
    select 1 from generate_series(events.start_date, :target_end_date, events.repeat) as d(rep_date) 
    where rep_date between :target_date and :target_end_date
);

I didn't figure out how to do it quickly.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question