diff --git a/superset/connectors/sqla/models.py b/superset/connectors/sqla/models.py index 1f68dea2be3de..cd93d11969a92 100644 --- a/superset/connectors/sqla/models.py +++ b/superset/connectors/sqla/models.py @@ -1087,12 +1087,12 @@ def get_sqla_query( # pylint: disable=too-many-arguments,too-many-locals,too-ma columns = groupby or columns for selected in columns: # if groupby field/expr equals granularity field/expr - table_col = columns_by_name.get(selected) - if table_col and table_col.type_generic == GenericDataType.TEMPORAL: - outer = table_col.get_timestamp_expression(time_grain, selected) + if selected == granularity: + sqla_col = columns_by_name[selected] + outer = sqla_col.get_timestamp_expression(time_grain, selected) # if groupby field equals a selected column - elif table_col: - outer = table_col.get_sqla_col() + elif selected in columns_by_name: + outer = columns_by_name[selected].get_sqla_col() else: outer = literal_column(f"({selected})") outer = self.make_sqla_column_compatible(outer, selected)