У меня есть две таблицы, подобные этой:
class AnnualMinMaxDim(db.Model):
annual_id = db.Column(db.Integer, primary_key=True)
area_id = db.Column(db.Integer)
sensor_id = db.Column(db.String)
annual_min_temp = db.Column(db.Integer)
annual_max_temp = db.Column(db.Integer)
annual_min_humidity = db.Column(db.Integer)
annual_max_humidity = db.Column(db.Integer)
annual_min_pressure = db.Column(db.Integer)
annual_max_pressure = db.Column(db.Integer)
from_date = db.Column(db.Date)
to_date = db.Column(db.Date)
class AnnualAvgDim(db.Model):
annual_avg_id = db.Column(db.Integer, primary_key=True)
area_id = db.Column(db.Integer)
sensor_id = db.Column(db.String)
annual_avg_temp = db.Column(db.Integer)
annual_avg_humidity = db.Column(db.Integer)
annual_avg_pressure = db.Column(db.Integer)
from_date = db.Column(db.Date)
to_date = db.Column(db.Date)
Мои требования:
- На основе запроса area_id необходимо выполнить.
- После прохождения area_id должны возвращаться минимальные, максимальные и средние значения температуры, давления и влажности из обеих таблиц.
Что я пробовал:
def get_AreaAnnual(find_by_entry):
try:
get_all_data = AnnualAvgDim.query.filter_by(area_id=find_by_entry).order_by(asc('to_date')).all()
get_min_max_data = AnnualMinMaxDim.query.filter_by(area_id=find_by_entry).order_by(asc('to_date')).all()
get_all_data_schema = AnnualAvgDimSchema(many=True).dump(get_all_data)
get_min_max_data_schema = AnnualMinMaxDimSchema(many=True).dump(get_min_max_data)
for annual_avg_dim, annual_minmax_dim in zip(get_all_data_schema, get_min_max_data_schema):
annual_avg_dim.update(annual_minmax_dim)
return get_all_data_schema
except Exception:
return dict(Unsucessfull="Unable to process your request, please try after some time")
Текущий выход:
[
{ 'annual_avg_pressure': 39, 'from_date': '2000-01-01', 'annual_avg_id': 42, 'to_date': '2000-12-
31','annual_avg_humidity': 73, 'area_id': 1026, 'annual_avg_temp': 33, 'annual_min_temp': 22,
'annual_min_pressure': 21, 'annual_id': 41, 'annual_min_humidity': 56, 'annual_max_pressure': 54,
'annual_max_humidity': 84, 'annual_max_temp': 44, sensor_id = "1aqwe3" },
{'annual_avg_pressure': 36, 'from_date': '2000-01-01', 'annual_avg_id': 57, 'to_date': '2000-12-31',
'annual_avg_humidity': 68, 'area_id': 1026, 'annual_avg_temp': 33, 'annual_min_temp': 24,
'annual_min_pressure': 20, 'annual_id': 56, 'annual_min_humidity': 51, 'annual_max_pressure': 54,
'annual_max_humidity': 79, 'annual_max_temp': 44, sensor_id = "2bqwe3"},
{'annual_avg_pressure': 34, 'from_date': '2002-01-01','annual_avg_id': 39, 'to_date': '2002-12-31',
'annual_avg_humidity': 65, 'room_area_id': 1026, 'annual_avg_temp': 33, 'annual_min_temp': 23,
'annual_min_pressure': 20, 'annual_id': 38, 'annual_min_humidity': 52, 'annual_max_pressure': 46,
'annual_max_humidity': 80, 'annual_max_temp': 44, sensor_id = "1aqwe3"},
{'annual_avg_pressure': 39, 'from_date': '2002-01-01', 'annual_avg_id': 54, 'to_date': '2002-12-31',
'annual_avg_humidity': 71, 'area_id': 1026, 'annual_avg_temp': 29, 'annual_min_temp': 20,
'annual_min_pressure': 20, 'annual_id': 53, 'annual_min_humidity': 54, 'annual_max_pressure': 53,
'annual_max_humidity': 82, 'annual_max_temp': 40, sensor_id = "2bqwe3"}
Ожидаемый результат
[
{ 'annual_avg_pressure': 37.5, 'from_date': '2000-01-01', 'annual_avg_id': 42, 'to_date': '2000-12-
31','annual_avg_humidity': 70.5, 'annual_avg_temp': 33, 'annual_min_temp': 23,
'annual_min_pressure': 20.5, 'annual_min_humidity': 53.5, 'annual_max_pressure': 54,
'annual_max_humidity': 81.5, 'annual_max_temp': 44},
{ 'annual_avg_pressure': 36.5, 'from_date': '2002-01-01', 'to_date': '2002-12-
31','annual_avg_humidity': 68, 'annual_avg_temp': 31, 'annual_min_temp': 21.5,
'annual_min_pressure': 21, 'annual_id': 41, 'annual_min_humidity': 53, 'annual_max_pressure': 49.5,
'annual_max_humidity': 81, 'annual_max_temp': 42}
Если мы увидим ожидаемый результат, я хочу консолидировать значения min, max и avg согласно to_date, что означает в этом году.