armanddemasson commited on
Commit
ff65346
·
1 Parent(s): a4206dc

feat: updated sql query for not macro countries in talk to ipcc

Browse files
climateqa/engine/talk_to_data/ipcc/queries.py CHANGED
@@ -52,9 +52,14 @@ def indicator_per_year_at_location_query(
52
  else:
53
  table_path = f"'{IPCC_DATASET_URL}/{table.lower()}/{country_code}.parquet'"
54
  sql_query = f"""
55
- SELECT year, scenario, AVG({indicator_column}) AS {indicator_column}
56
- FROM {table_path}
57
- WHERE latitude = {latitude} AND longitude = {longitude} AND year >= 1950
 
 
 
 
 
58
  GROUP BY scenario, year
59
  ORDER BY year, scenario
60
  """
@@ -107,10 +112,16 @@ def indicator_for_given_year_query(
107
  else:
108
  table_path = f"'{IPCC_DATASET_URL}/{table.lower()}/{country_code}.parquet'"
109
  sql_query = f"""
110
- SELECT AVG({indicator_column}) AS {indicator_column}, latitude, longitude, scenario
111
- FROM {table_path}
112
- WHERE year = {year}
 
 
 
 
 
113
  GROUP BY latitude, longitude, scenario
114
  ORDER BY latitude, longitude, scenario
115
  """
 
116
  return sql_query.strip()
 
52
  else:
53
  table_path = f"'{IPCC_DATASET_URL}/{table.lower()}/{country_code}.parquet'"
54
  sql_query = f"""
55
+ WITH medians_per_month AS (
56
+ SELECT year, scenario, month, MEDIAN({indicator_column}) AS median_value
57
+ FROM {table_path}
58
+ WHERE latitude = {latitude} AND longitude = {longitude} AND year >= 1950
59
+ GROUP BY scenario, year, month
60
+ )
61
+ SELECT year, scenario, AVG(median_value) AS {indicator_column}
62
+ FROM medians_per_month
63
  GROUP BY scenario, year
64
  ORDER BY year, scenario
65
  """
 
112
  else:
113
  table_path = f"'{IPCC_DATASET_URL}/{table.lower()}/{country_code}.parquet'"
114
  sql_query = f"""
115
+ WITH medians_per_month AS (
116
+ SELECT latitude, longitude, scenario, month, MEDIAN({indicator_column}) AS median_value
117
+ FROM {table_path}
118
+ WHERE year = {year}
119
+ GROUP BY latitude, longitude, scenario, month
120
+ )
121
+ SELECT latitude, longitude, scenario, AVG(median_value) AS {indicator_column}
122
+ FROM medians_per_month
123
  GROUP BY latitude, longitude, scenario
124
  ORDER BY latitude, longitude, scenario
125
  """
126
+
127
  return sql_query.strip()