Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pypika_tortoise/queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -1662,7 +1662,7 @@ def _group_sql(
clauses.append(select.get_sql(ctx))
break
else:
clauses.append(field.get_sql(ctx))
clauses.append(field.get_sql(ctx.copy(with_alias=False)))

sql = " GROUP BY {groupby}".format(groupby=",".join(clauses))

Expand Down
12 changes: 12 additions & 0 deletions tests/test_selects.py
Original file line number Diff line number Diff line change
Expand Up @@ -740,6 +740,18 @@ def test_groupby__multi_with_totals(self):

self.assertEqual('SELECT "foo","bar" FROM "abc" GROUP BY "foo","bar" WITH TOTALS', str(q))

def test_groupby_field_alias_ignored_in_subquery(self):
# When a query with a GROUP BY on a field that has an alias is used as a
# subquery in a FROM clause, the GROUP BY should not include the field
# alias (it should render the field expression only).
inner = Query.from_(self.t).select(self.t.foo).groupby(self.t.bar.as_("bar_alias"))
parent = Query.from_(inner).select("*")

self.assertEqual(
'SELECT * FROM (SELECT "foo" FROM "abc" GROUP BY "bar") "sq0"',
str(parent),
)


class HavingTests(unittest.TestCase):
table_abc, table_efg = Tables("abc", "efg")
Expand Down