title: How do I get the time period start time using SQL over PI-ODBC?
body: <p>I'm using the most recent PI-OLEDB library to read data from aggregate views in OSIsoft PI Historian into SQL Server. Example:</p>
  
  <pre><code>SELECT time, value 
  FROM piavg
  WHERE 
     timestep = RELDATE('1h') 
     AND tag = TAGNAME('mytag')
     AND time &gt; DATE('4-Mar-12 00:00:00');
  </code></pre>
  
  <p>Unfortunately, the aggregate views (PIavg, etc.) only provide a single <code>time</code> column, which represents the <em>end</em> of the period specified by the <code>timestep</code> column.</p>
  
  <p>How can I retrieve the start time as well for the same period? I know PI-SQL supports some funky date math literals, but I can't quite figure out the syntax for <code>time - RELDATE('1h')</code> or whatever that can then be aliased as the <code>starttime</code>.</p>
  
  <p>(Caveat: I don't use PI, so I'm flying blind and can't just trial-and-error this. I have the PI OLEDB Data Provider Manual, but it's pretty sparse on details.)</p>
  
  <p>I realize I could cobble something together in SQL Server, but I'd rather use PI date functions so when SQL Server gets the data back there's no additional work needed. I'm working with a number of <code>timestep</code> values, so it's not just a static DATEADD() in SQL Server.</p>