Как рассчитать минимальное, максимальное и среднее значения за год по Id - PullRequest
0 голосов
/ 04 марта 2020

Я хочу вычислить минимальные, максимальные и средние значения для года a area_id.

для area_id и Year, доступны дубликаты.

пример данных:

[

 { '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}
   ]

Что я пробовал:

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")
...