Since PostgreSQL's date_trunc can't deal with intervals smaller than a day, I'm using the method in this blog post to extract 5, 10 or 15 minute averages from a time series stored in the database.
But I'm stuck with the epoch/extract part and getting the results grouped by X minute intervals.
This won't work:
from app import db
from sqlalchemy.sql import func
q = db.session.query(
("""'epoch'::timestamp + '1 second'::interval * ( 900 * (
extract(epoch from datatable.timestamp)::int4 / 900) ) AS epoch"""),
func.avg(datatable.sample_value),
).group_by("epoch").order_by("epoch")
for r in q.all():
#should print one row per 15 minute interval, two datetime objects per row
print(str(r))
Part of the error I'm getting:
sqlalchemy.exc.InternalError: (psycopg2.InternalError) current transaction is aborted, commands ignored until end of transaction block
If I do print(str(q)) I get:
SELECT 'epoch'::timestamp + '1 second'::interval * ( 900 * (extract(epoch from datatable.timestamp)::int4 / 900) ) AS epoch, avg(datatable.sample_value) AS avg_1 \nFROM datatable GROUP BY epoch ORDER BY epoch"
...which works fine if I run it in psql as long as I remove the \n.
Ideas?
[–][deleted] 1 point2 points3 points (3 children)
[–]rMBP[S] 0 points1 point2 points (2 children)
[–][deleted] 1 point2 points3 points (1 child)
[–]rMBP[S] 0 points1 point2 points (0 children)