I’m using the most recent PI-OLEDB library to read data from aggregate views in OSIsoft PI Historian into SQL Server. Example:
SELECT time, value
FROM piavg
WHERE
timestep = RELDATE('1h')
AND tag = TAGNAME('mytag')
AND time > DATE('4-Mar-12 00:00:00');
Unfortunately, the aggregate views (PIavg, etc.) only provide a single time column, which represents the end of the period specified by the timestep column.
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 time - RELDATE('1h') or whatever that can then be aliased as the starttime.
(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.)
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 timestep values, so it’s not just a static DATEADD() in SQL Server.
It turns out that
time - RELDATE('1h')works perfectly.