title: SQL Syntax IF/THEN/ELSE
body: <p>So I've been dreading asking this question - mostly because I'm terrible at logic in excel, and transferring logic statements to SQL is such a struggle for me, but I'm going to try and make this as clear as possible.</p>
  
  <p>I have two tables. One table is <code>historic_events</code> and the other is <code>future_events</code>. Based on <code>future_events</code>, I have another table <code>confidence_interval</code> that calculates a z-score telling me based on how many <code>future_events</code> will occur, how many <code>historic_event</code> data points I will need to calculate a reliable average. Each record in <code>historic_events</code> has a unique key called <code>event_id</code>. Each record in <code>confidence_interval</code> has a field called <code>service_id</code> that is unique. The 'service_id' field also exists in 'historic_events' and they can be joined on that field.</p>
  
  <p>So, with all that being said, based on the count of future events by <code>service_id</code>, my <code>confidence_interval</code> table calculates the z-score. I then need to select records from the <code>historic_events</code> table for each <code>service_id</code> that satisfy the following parameters</p>
  
  <pre><code>Select * EVENT_ID 
  From historic_events
  where END_DATE is within two calendar years from todays date
  and count of `EVENT_ID` is &gt;= `confidence_interval.Z_SCORE`
  </code></pre>
  
  <p>if those parameters are not met, then I want to widen the date value to being within three years. </p>
  
  <p>if those parameters are still not met, I want to widen the date value to being within four years, and then again to five years. If there still aren't enough datapoints after five years, ohwell, we'll settle for what we have. We do not want to look at datapoints that are older than five years.</p>
  
  <p>I want my end result to be a table that has a list of the <code>EVENT_ID</code> and I would re-run the SQL query for each <code>service_id</code>.</p>
  
  <p>I hope this makes sense - I can figure out the <code>SELECT</code> and <code>FROM</code>, but totally getting stuck on the <code>WHERE</code>.</p>