I am currently using the following sqlalchemy code,_query = super().get_query(model, info, sort, **args)query = _query.group_by(func.date_format(DataModel.timestamp, "%Y-%m-%d %H"))return queryI am trying to aggregate the the max value of a field from DataModel.value utilizing the group by clause.In simple sql, you would do the following.SELECT max(data.value) AS data_valuefrom dataGROUP BY date_format(data.timestamp, "%Y-%m-%d %H")What would the proper way to express this? The various methods I have tried somehow overwrite the original query and do not map to our attributes.
I have some additional context on the issue
- We are using the graphene-sqlalchemy package.
- When you do a GraphQL web api query, the package creates a sqlalchemy.orm.query.Query object.
- We want to modify this standard query that the package creates so that we can do the group_by action to help us get the maximum time series value for every hour because this is not possible using GraphQL.
- Talking to the graphene-sqlalchemy team they told us the best place to do the modification to the standardized query is in the get_query method (line 67) in this file: https://github.com/graphql-python/graphene-sqlalchemy/blob/master/graphene_sqlalchemy/fields.py
- The standardized query we need to modify translates to this SQL statement:
SELECT sy_data.oid_id, sy_data.rrd_timestamp, sy_data.cabinet_id, sy_data
.customer_id, sy_data.value, sy_data.active_flag, sy_data.timestamp
FROM sy_data
WHERE sy_data.rrd_timestamp >= %(rrd_timestamp_1)s AND sy_data.rrd_timestamp <= %(rrd_timestamp_2)s AND (sy_data.oid_id = %(oid_id_1)s OR sy_data.oid_id = %(o
id_id_2)s) ORDER BY sy_data.oid_id ASC, sy_data.rrd_timestamp ASCTherefore we need to find a way to insert a func.max for the values in the first part of the SELECT statement, (before the FROM). It is easy for us to apend the group_by like this.
query.group_by(func.date_format(DataModel.timestamp, "%Y-%m-%d %H"))The big issue for us is to figure out how to insert the func.max
Thanks Mike,Ideally we'd prefer to find a solution via Graphene-SQLAlchemy. Unfortunately we don't have the luxury of creating our own query when interacting with Graphene-SQLAlchemy.So the key question for us is, can you modify an existing sqlalchemy.orm.query.Query object to insert a "func.max(Data.value)" object into the select?
This is what Graphene-SQLAlchemy is giving us.If this is possible, then the group_by part is easy. We have tested that frequently, the hard part is the modifying the original "select" object.1. We have tried add_columns, but that adds in incompatible object type in the GraphQL results making it an unusable option.
2. We thought that modifying the select would be possible using data with "statement.froms" but can't figure out how to update the MetaData object in it
If modifying the "select" after its creation is not possible, we need to start considering using a separate reporting table with hourly data.
Mike,Would this select query be able to get our aggregated data?query = select(func.max(DataModel.value)).select_from(_query.subquery()).group_by(func.date_format(DataModel.timestamp, "%Y-%m-%d %H"))We tried this route but are now getting this error"errors": [{"message": "'Select' object has no attribute 'statement'","locations": [{"line": 2,"column": 3}],