title: Recurring Events, SQL Query
body: <p>I am aware that questions about recurring events are common but I have not been able to find one with an answer to this specific question about recurring events other than those to do with calendar apps. The main difference being that the events in our app. will only ever be seen either in reports or by themselves rather than in a calendar format although in many ways they are very similar, maybe just with less of the baggage associated with calendars.</p>
  <p>In a similar way to a calendar app. events can either occur on a one-off basis or can be recurring E.g. every Thursday or first Monday of every month, until some pre-set time in the future.</p>
  <p>The events are stored in an event table which contains the start and end dates and the 'recurrency type id'. If the 'recurrency type' is 'None' then the start and end dates will be the same. The event table holds an id to a separate table which holds the event type name, E.g. 'Meeting' or 'Weekly report'</p>
  <p>There is a further table which contains the list of 'recurrency types' E.g. 'No recurrence', 'Every Monday', 'First Monday of month' and 'Last Saturday of month'.</p>
  <p>To make lookups easier another table contains a list of dates from 1960 to 2060 along with relevant information about each date, like whether it is a Monday, and which occurrence of Monday it is in the month.</p>
  <p>This allows a lookup like:
  <code><pre>
  SELECT DISTINCT(e.eventid),n.nameid,n.firstname,n.lastname,d.dt,r.recurring
  FROM dates d
  LEFT JOIN recurringtypes r
  /* if event recurring every week E.g. 'Every Monday' */
  ON (r.rectypeid BETWEEN 2 AND 8 AND r.day = d.dow)
  /* if event recurring every month E.g. 'First Monday, every month' */
  OR ((r.rectypeid BETWEEN 9 AND 36) AND r.day = d.dow AND r.occurrence = d.occurrence)
  /* if event recurring every last week of month E.g. 'Last Monday, every month' */
  OR (r.rectypeid &gt;= 37 AND r.day = d.dow and r.islast = d.islast)
  LEFT JOIN events e on e.rectypeid = r.rectypeid
  LEFT JOIN eventtypes t ON e.eventtypeid = t.eventtypeid
  LEFT JOIN names n ON e.namesid = n.namesid
  WHERE (d.dt BETWEEN '2012/02/01' AND '2012/05/01')
  ORDER BY d.dt;
  </pre></code></p>
  <p>Which is exactly what is required for finding recurring events, giving output like:
  <code><pre>
  +-----------+---------------+-------------------+-----------+------------+-------------------------------+
  | eventid   | nameid        | lastname          | firstname | dt         | recurring                     |
  +-----------+---------------+-------------------+-----------+------------+-------------------------------+
  |   3291788 |       1728449 | smith             | zoe       | 2012-02-02 | First Thursday, every month   |
  |   3291797 |       1765432 |                   |           | 2012-02-05 | First Sunday, every month     |
  |   3291798 |       1730147 |                   |           | 2012-02-05 | First Sunday, every month     |
  |   3291803 |       1790061 | Carpenter         | Richie    | 2012-02-06 | Every Monday                  |
  |   3291805 |       1790061 | Carpenter         | Richie    | 2012-02-08 | Second Wednesday, every month |
  |   3291803 |       1790061 | Carpenter         | Richie    | 2012-02-13 | Every Monday                  |
  |   3291799 |       1790061 | Carpenter         | Richie    | 2012-02-15 | Third Wednesday, every month  |
  |   3291803 |       1790061 | Carpenter         | Richie    | 2012-02-20 | Every Monday                  |
  </pre></code></p>
  <p>To get none recurring events a simpler query can be used:
  <code><pre>
  SELECT n.nameid,n.lastname,n.firstname,e.firstdate,e.eventid,'No' as Recurring
  FROM events e
  LEFT JOIN names n ON n.names = e.namesid
  AND e.rectypeid &lt;= 1
  AND e.firstdate BETWEEN '2012/02/01' AND '2012/05/01'
  AND e.eventid IS NOT NULL ORDER BY e.firstdate;
  </pre></code>
  This gives output very similar to the first query but, crucially, the dates are from the events table rather than the dates table.</p>
  <p><strong>My question is: How can I combine these queries to come up with one list that contains all the events, both recurring and non-recurring in date order?</strong></p>
  <hr />
  <p>These are the tables and shortened selections from them, some columns and all indexes have been removed for brevity :). The 'names' table has not been included for the same reason.
  <code><pre>
  CREATE TABLE events (
  eventid int(11) NOT NULL AUTO_INCREMENT,
  eventtypeid int(11) DEFAULT '0',
  firstdate date DEFAULT '1960-01-01' COMMENT 'First event',
  lastdate date DEFAULT '1960-01-01' COMMENT 'Last event',
  rectypeid int(11) DEFAULT '1'
  );
  +---------+-------------+------------+------------+-----------+
  | eventid | eventtypeid | firstdate  | lastdate   | rectypeid |
  +---------+-------------+------------+------------+-----------+
  | 3291803 |          16 | 2012-02-03 | 2012-04-11 |         3 |
  | 3291797 |           8 | 2012-02-12 | 2012-02-22 |         9 |
  | 3291798 |           5 | 2012-02-12 | 2012-02-12 |         9 |
  | 3291788 |           8 | 2012-05-24 | 2015-01-16 |        13 |
  | 3291805 |          10 | 2012-01-04 | 2012-02-14 |        19 |
  | 3291799 |          16 | 2012-02-07 | 2012-10-24 |        26 |
  | 3291804 |           5 | 2012-02-03 | 2012-08-22 |        41 |
  +---------+-------------+------------+------------+-----------+
  CREATE TABLE cmseventtypes (
  eventtypeid int(11) NOT NULL AUTO_INCREMENT,
  eventtype varchar(50) DEFAULT '' COMMENT 'Event type AKA name'
  );
  +-------------+----------------------+
  | eventtypeid | eventype             |
  +-------------+----------------------+
  |           1 | Follow up meeting    |
  |           2 | Reminder email due   |
  |           3 | Monthly meeting      |
  |           4 | Weekly report        |
  |           5 | Golf practice        |
  +------------------------------------+
  CREATE TABLE recurringtypes (
  rectypeid int(11) NOT NULL AUTO_INCREMENT,
  recurring varchar(40) DEFAULT '',
  day tinyint(4) DEFAULT '0',
  occurrence tinyint(4) DEFAULT '0',
  islast tinyint(4) DEFAULT '0'
  );
  +-----------+---------------------------+------+------------+--------+
  | rectypeid | recurring                 | day  | occurrence | islast |
  +-----------+---------------------------+------+------------+--------+
  |         1 | No                        |    0 |          0 |      0 |
  |         2 | Every Sunday              |    1 |          0 |      0 |
  |         3 | Every Monday              |    2 |          0 |      0 |
  |         4 | Every Tuesday             |    3 |          0 |      0 |
  |         5 | Every Wednesday           |    4 |          0 |      0 |
  |         6 | Every Thursday            |    5 |          0 |      0 |
  |         7 | Every Friday              |    6 |          0 |      0 |
  |         8 | Every Saturday            |    7 |          0 |      0 |
  |         9 | First Sunday, every month |    1 |          1 |      0 |
  |        10 | First Monday, every month |    2 |          1 |      0 |
  +-----------+---------------------------+------+------------+--------+
  CREATE TABLE dates (
  dt date NOT NULL COMMENT 'Date',
  daycount mediumint(9) NOT NULL DEFAULT '1',
  year smallint(6) NOT NULL DEFAULT '1970',
  month tinyint(4) NOT NULL DEFAULT '1',
  dom tinyint(4) NOT NULL DEFAULT '1',
  dow tinyint(4) NOT NULL DEFAULT '1',
  occurrence tinyint(4) NOT NULL DEFAULT '0',
  islast tinyint(1) NOT NULL DEFAULT '0'
  );
  +------------+----------+------+-------+-----+-----+------------+--------+
  | dt         | daycount | year | month | dom | dow | occurrence | islast |
  +------------+----------+------+-------+-----+-----+------------+--------+
  | 2012-02-02 |   734900 | 2012 |     2 |   2 |   5 |          1 |      0 |
  | 2012-02-03 |   734901 | 2012 |     2 |   3 |   6 |          1 |      0 |
  | 2012-02-04 |   734902 | 2012 |     2 |   4 |   7 |          1 |      0 |
  | 2012-02-05 |   734903 | 2012 |     2 |   5 |   1 |          1 |      0 |
  | 2012-02-06 |   734904 | 2012 |     2 |   6 |   2 |          1 |      0 |
  | 2012-02-07 |   734905 | 2012 |     2 |   7 |   3 |          1 |      0 |
  | 2012-02-08 |   734906 | 2012 |     2 |   8 |   4 |          2 |      0 |
  | 2012-02-09 |   734907 | 2012 |     2 |   9 |   5 |          2 |      0 |
  +------------+----------+------+-------+-----+-----+------------+--------+
  </pre></code></p>
  <hr />
  <p>We are not absolutely set on using the above code or table layout, any working solutions would be welcome. Please do not point me towards:</p>
  <p><a href="https://stackoverflow.com/questions/430012/how-would-you-store-possibly-recurring-times">How would you store possibly recurring times?</a></p>
  <p><a href="https://stackoverflow.com/questions/85699/whats-the-best-way-to-model-recurring-events-in-a-calendar-application">What&#39;s the best way to model recurring events in a calendar application?</a></p>
  <p><a href="https://stackoverflow.com/questions/4239871/when-building-a-calendar-app-should-i-store-dates-or-recurrence-rules-in-my-database">Should I store dates or recurrence rules in my database when building a calendar app?</a></p>
  <p>or</p>
  <p><a href="https://www.rfc-editor.org/rfc/rfc5545" rel="nofollow noreferrer">https://www.rfc-editor.org/rfc/rfc5545</a></p>
  <p>I have checked them out and they were very useful but not doing the same as we intend.</p>
  <p>TIA</p>