0

I am facing this problem with a calendar.

I have to extract all records (events) (via select) with: 1. a specific date, or 2. repeatable date

The problem is that a normal calendar displays the entire month (or specific range of time), so it is possible to select a record from a subset generated from a subquery (and compare the date fragments), i.e.:

select (generate_series('2012-06-29 00:00:00',
                        '2012-07-03 00:00:00', 
                        '5 minutes'::interval))::timestamp;

I have to build the view of a calendar like an infinite list of events, that you can scroll down. So I have to display events one-by-one. When I select the events (i.e. 2012-06-29 00:00:00 to 2012-10-29 00:00:00), then the statement will not consider the record with date 2012-10-30 00:00:00, which is not expected.

How can I select multiple, non-continuous dates?

The database schema:

CREATE TABLE "public"."events" (
"id" int4 DEFAULT nextval('events_id_seq'::regclass) NOT NULL,
"date" timestamp(6),
"date_repeat_interval" interval(6),
"date_repeat_start" timestamp(6),
"date_repeat_stop" timestamp(6),
"event_name" varchar(255) NOT NULL
)
WITH (OIDS=FALSE);

Insert some regular events on a specific date:

INSERT INTO "public"."events" VALUES ('1', '2013-04-18 14:04:39', null, null, null, 'Regular 1');
INSERT INTO "public"."events" VALUES ('2', '2013-04-19 14:04:50', null, null, null, 'Regular 2');

And insert some events with interval 1 and 2 days and specified repetition from and to date:

INSERT INTO "public"."events" VALUES ('3', null, '1 day', '2013-04-16 14:05:26', '2013-04-19 14:05:31', 'Repeatable 1');
INSERT INTO "public"."events" VALUES ('4', null, '2 days', '2013-04-17 14:05:49', '2013-06-15 14:05:53', 'Repeatable 2');

Question:

How to query database about all events in range: from NOW to infinity with LIMIT 10. that they occurs in order of occurence including multiple occurence for repeatable events.

4
  • 1
    In it's current form your question doesn't make sense. Why would you expect a timestamp outside the range you specify to be considered? Can you show table definitions and your sql query? You might want to make an sqlfiddle sqlfiddle.com
    – Eelke
    Apr 18, 2013 at 5:28
  • What is a repeatable date? Apr 18, 2013 at 11:24
  • Sure, I will reveal the topic. When We have repeatable events in your calendar and we want to present this not in range: day, week, month, year (where the time is in a specific range), but we want to present them as a scrollable list (to infinity), 10 items per page, there is the problem, how to query a database to display these events. You cannot close the range from now to X, because there may be an event on date X+1 minute. I have updated the question and included database schema with briefly explained problem.
    – Athlan
    Apr 18, 2013 at 12:10
  • As I'm not really sure why you would not expect the date 2012-10-30 to be skipped (as it is outside the range you provided), I'll suggest another way of thinking about the problem. I suggest not thinking about it as an SQL problem. Ask yourself: Can I generate the list (or boundaries) of the date range at all? If so, it's a simple matter of JOINs and maybe some UNIONs. If not, you do not have an SQL problem, but a problem to formulate the requirement. And, of course, there's always the possibility of SELECTing everything and weeding out on the application level.
    – 0xCAFEBABE
    Apr 18, 2013 at 14:29

1 Answer 1

0

You can do this with a recursive CTE. Note you really need to add limits to it or it will happily repeat to infinity.

WITH RECURSIVE event_calendar AS (
    SELECT id, coalesce("date", date_repeat_start) as e_date, event_name, 0 AS level
      FROM events
     UNION
    SELECT e.id, ec.e_date + date_repeat_interval, ec.event_name, ec.level + 1
      FROM events e
      JOIN event_calendar ec ON e.id = ec.id
     WHERE ec.level <= 10 and e.date_repeat_start is not null 
           and e.date_repeat_stop >= ec.e_date + date_repeat_interval
)
SELECT * FROM event_calendar order by e_date;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.