diff --git a/flowmachine/flowmachine/core/join_to_location.py b/flowmachine/flowmachine/core/join_to_location.py index 063e44945e..cd2638bcdc 100644 --- a/flowmachine/flowmachine/core/join_to_location.py +++ b/flowmachine/flowmachine/core/join_to_location.py @@ -241,16 +241,20 @@ def _get_site_query(self, *, size, polygon_table, geom_col): def column_names(self) -> List[str]: right_columns = get_columns_for_level(self.level, self.column_name) left_columns = self.left.column_names - if "location_id" in right_columns and "location_id" in left_columns: - left_columns.remove("location_id") + for column in right_columns: + if column in left_columns: + left_columns.remove(column) return left_columns + right_columns def _make_query(self): - right_columns = get_columns_for_level(self.level, self.column_name) + right_columns = self.right_query.column_names left_columns = self.left.column_names - if "location_id" in right_columns and "location_id" in left_columns: - left_columns.remove("location_id") + print(right_columns) + print(left_columns) + for column in right_columns: + if column in left_columns: + left_columns.remove(column) right_columns_str = ", ".join([f"sites.{c}" for c in right_columns]) left_columns_str = ", ".join([f"l.{c}" for c in left_columns]) diff --git a/flowmachine/flowmachine/features/network/total_network_objects.py b/flowmachine/flowmachine/features/network/total_network_objects.py index ecc3f837c7..e134b39d72 100644 --- a/flowmachine/flowmachine/features/network/total_network_objects.py +++ b/flowmachine/flowmachine/features/network/total_network_objects.py @@ -153,24 +153,22 @@ def column_names(self) -> List[str]: ] def _make_query(self): - cols = ",".join(get_columns_for_level(self.network_object)) - group_cols = ",".join( - get_columns_for_level(self.level, self.joined.column_name) - ) - sql = """ - SELECT {group_cols}, COUNT(*) as total, + cols = get_columns_for_level(self.network_object) + group_cols = get_columns_for_level(self.level, self.joined.column_name) + for column in group_cols: + if column in cols: + cols.remove(column) + cols_str = ",".join(cols) + group_cols_str = ",".join(group_cols) + sql = f""" + SELECT {group_cols_str}, COUNT(*) as total, datetime FROM - (SELECT DISTINCT {group_cols}, {cols}, datetime FROM - (SELECT {group_cols}, {cols}, date_trunc('{total_by}', x.datetime) AS datetime - FROM ({etu}) x) y) _ - GROUP BY {group_cols}, datetime - ORDER BY {group_cols}, datetime - """.format( - total_by=self.total_by, - etu=self.joined.get_query(), - cols=cols, - group_cols=group_cols, - ) + (SELECT DISTINCT {group_cols_str}, {cols_str}, datetime FROM + (SELECT {group_cols_str}, {cols_str}, date_trunc('{self.total_by}', x.datetime) AS datetime + FROM ({self.joined.get_query()}) x) y) _ + GROUP BY {group_cols_str}, datetime + ORDER BY {group_cols_str}, datetime + """ return sql