I am pretty new in Python and SQL Alchemy and I have a question about a query that I am trying to do.
More specifically I have this data list. It contains some measures every 5 mins. What I would like to do is to group by timestamp, based on 30 mins, so when:
and then aggregate the data on that value.
Now I have the code below:
base_query = (
db.query(
MyTable.timestamp,
func.max(MyTable.dual).label("max"),
func.min(MyTable.dual).label("min"),
func.avg(MyTable.dual).label("avg"),
func.count(MyTable.id).label("count"),
)
.join(Simulation, Simulation.id == MyTable.simulation_id)
.filter(
and_(MyTable.dual > 0, Simulation.scenario_id == scenario_id)
)
.group_by(MyTable.timestamp)
.order_by(MyTable.timestamp)
)
timestamp count avg
"2020-01-13 00:00:00+00" "10" "6454.70683325249"
"2020-01-13 00:05:00+00" "10" "6454.70683325249"
"2020-01-13 00:10:00+00" "10" "6454.70683325249"
"2020-01-13 00:15:00+00" "10" "6454.70683325249"
"2020-01-13 00:20:00+00" "10" "6454.70683325249"
"2020-01-13 00:25:00+00" "10" "6454.70683325249"
"2020-01-13 00:30:00+00" "10" "6454.70683325249"
"2020-01-13 00:35:00+00" "10" "6454.70683325249"
"2020-01-13 00:40:00+00" "10" "6454.70683325249"
"2020-01-13 00:45:00+00" "10" "6454.70683325249"
"2020-01-13 00:50:00+00" "10" "6454.70683325249"
"2020-01-13 00:55:00+00" "10" "6454.70683325249"
"2020-01-13 01:00:00+00" "10" "6454.70683325249"
"2020-01-13 01:05:00+00" "10" "6454.70683325249"
"2020-01-13 01:10:00+00" "10" "6454.70683325249"
"2020-01-13 01:15:00+00" "10" "6454.70683325249"
Firstly, we must figure out how to do it in sql. Getting half an hour intervals should not be too difficult:
select 1800*(floor(unix_timestamp(timestamp)/1800))
You might want convert this back to datetime, but I am afraid that's not portable enough. In mysql you can use from_unixtime, but if you get the unix timestamp as the result of the query, you can easily convert it to anything you want in python.
Secondly, how to do it in sqlalchemy. It looks like it is pretty straightforward:
import sqlalchemy
from sqlalchemy import select, func
q = select([
(1800*func.floor(func.unix_timestamp(t.c.tm0)/1800)).label("tm_hh")
]))
print(q)
# prints out this:
# SELECT %s * floor(unix_timestamp(T.tm0) / %s) AS tm_hh \nFROM T
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.