[Django] #30633: Group by concat(field1, field2) producing wrong result

7 views
Skip to first unread message

Django

unread,
Jul 11, 2019, 7:15:22 PM7/11/19
to django-...@googlegroups.com
#30633: Group by concat(field1,field2) producing wrong result
-----------------------------------------+--------------------------------
Reporter: bishwadeep | Owner: nobody
Type: Uncategorized | Status: new
Component: Uncategorized | Version: 2.2
Severity: Normal | Keywords: mysql group by
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-----------------------------------------+--------------------------------
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.

--
Ticket URL: <https://code.djangoproject.com/ticket/30633>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Jul 12, 2019, 1:20:05 AM7/12/19
to django-...@googlegroups.com
#30633: Group by concat(field1,field2) producing wrong result.
-------------------------------------+-------------------------------------
Reporter: bishwadeep | Owner: nobody
Type: Uncategorized | Status: closed
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution: invalid

Keywords: mysql group by | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by felixxm):

* status: new => closed
* component: Uncategorized => Database layer (models, ORM)
* version: 2.2 => master
* resolution: => invalid


Comment:

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
[https://code.djangoproject.com/wiki/TicketClosingReasons/UseSupportChannels
support channels].

--
Ticket URL: <https://code.djangoproject.com/ticket/30633#comment:1>

Reply all
Reply to author
Forward
0 new messages