Opened 5 years ago

Closed 5 years ago

#30633 closed Uncategorized (invalid)

Group by concat(field1,field2) producing wrong result.

Reported by: bishwadeep Owned by: nobody
Component: Database layer (models, ORM) Version: dev
Severity: Normal Keywords: mysql group by
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I am new to Django and I am enjoying it. I ran into an issue while creating a SQL query for my project. I want the following query to run using Django
Query:
Select id,field1,field2,field3 FROM table WHERE field1 = somevalue
Group by concat(field1,field2) ORDER BY somefield

However I am not able to get the desired query. It keeps on adding group by id which is not what I want.
Django query:
result = query.annotate(person=Concat(F('field1'), F('field2'))).annotate(Count('person', distinct=True))
Outputs:
SELECT id FROM table WHERE my condition GROUP BY id ORDER BY field

Please let me know if you have any questions.

Change History (1)

comment:1 by Mariusz Felisiak, 5 years ago

Component: UncategorizedDatabase layer (models, ORM)
Resolution: invalid
Status: newclosed
Summary: Group by concat(field1,field2) producing wrong resultGroup by concat(field1,field2) producing wrong result.
Version: 2.2master

I'm not sure what exactly you want to achieve because your expected query and Django queryset are slightly different, but my understanding is that values() is missing:

query.annotate(person=Concat('field1', 'field2')).values('person').annotate(Count('person', distinct=True))

Moreover this is a support question not a bug in Django, please use one of support channels.

Note: See TracTickets for help on using tickets.
Back to Top