GROUP BY without using aggregate function

929 views
Skip to first unread message

marcin....@gmail.com

unread,
Mar 2, 2016, 12:06:35 PM3/2/16
to Django users
Hello,

I would like to query database with Django ORM and add GROUP BY clause without including unnecessary counting, avg, etc.
I can force/hack queryset by settting qs.query.group_by=['my_column'], but this is unclean and risky.

Is possible something like:

MyModel.objects.filter(...).group_by('my_column')

?

Kind Regards,
Marcin

Dan Tagg

unread,
Mar 2, 2016, 12:11:32 PM3/2/16
to django...@googlegroups.com
if your aren't counting or averaging then why are you grouping?

D

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/64f4b422-7704-413d-a9c5-837c53eaefc9%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Wildman and Herring Limited, Registered Office: 52 Great Eastern Street, London, EC2A 3EP, Company no: 05766374

marcin....@gmail.com

unread,
Mar 2, 2016, 12:21:03 PM3/2/16
to Django users


On Wednesday, March 2, 2016 at 6:11:32 PM UTC+1, Dan Tagg wrote:
if your aren't counting or averaging then why are you grouping?



To fast find distinct values within big dataset - some queries on some engines may be faster when using grouping. 
It was known technique years ago and I'm still using it.

Marcin

Michal Petrucha

unread,
Mar 3, 2016, 8:28:58 AM3/3/16
to django...@googlegroups.com
As far as I know, reasonable SQL engines require that every selected
column is either in GROUP BY, or wrapped in an aggregation function
(and on those that don't enforce this rule, the result contains
arbitrary values). I fail to see how a query like this could map to
model instances.

Perhaps if you could provide a more specific example of what kind of
query you're trying to build here?

Merely based on your vague description, it seems to me that this is
one of the cases where you might be better off writing your SQL query
manually.

Cheers,

Michal
signature.asc

marcin....@gmail.com

unread,
Mar 3, 2016, 8:39:50 AM3/3/16
to Django users


On Thursday, March 3, 2016 at 2:28:58 PM UTC+1, Michal Petrucha wrote:

Merely based on your vague description, it seems to me that this is
one of the cases where you might be better off writing your SQL query
manually.


Narrowing results by `filter()` is very handy, in contrast to writing raw sql. I need to add GROUP BY clause only.
To be honest I'm waiting for official support for SQLAlchemy in Django, because Django ORM is very limited.

But after a while I realized that I should verify performance differences between DISTINCT and GROUP BY.  Maybe today query planners are smart enough, so the "group by" trick can be abadoned.

The example of query is `select rel_id from some_table group by rel_id` (versus select distinct rel_id from some_table) 

Marcin


Reply all
Reply to author
Forward
0 new messages