Opened 8 years ago

Closed 8 years ago

Last modified 8 years ago

#26696 closed Cleanup/optimization (invalid)

QuerySet.extra: group by weekday on a date field

Reported by: László Károlyi Owned by: nobody
Component: Database layer (models, ORM) Version: 1.9
Severity: Normal Keywords: day of week weekday
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by László Károlyi)

Hey,

I'm coming from the Django doc, where you say that I should try to avoid using QuerySet.extra in the future, and report the thing I want to achieve here, so you could improve the ORM. Here's the thing:

I have a mysql table where there is a date field. I'd like to do a DAYOFWEEK(date) calculation on it, then GROUP BY its result, and then do some MIN/MAX/AVG calculation on another field in the table. The point is to get the minimum/maximum/average values for the days of the week.

Currently this does not seem possible with even using the extra parameter.

Or is it?

Tips welcomed, but I actually have given up on creating this query out of the ORM.

Change History (3)

comment:1 by László Károlyi, 8 years ago

Description: modified (diff)

comment:2 by Simon Charette, 8 years ago

Keywords: QuerySet.extra removed
Resolution: invalid
Status: newclosed

Hi Karolyi,

As you can't build the query using QuerySet.extra() in the first time I'm going to close this ticket as invalid. You should be able to annotate your queryset using Func(F('datefield'), function='DAYOFWEEK') (or ExtractWeekDay on Django 1.10+) and use values() before your min/max/avg annotate() call to group by your annotated day of week.

Please see the section about values() in the aggregation docs for more details and our support channels.

comment:3 by László Károlyi, 8 years ago

Thx @charettes,

I got helped on #django-dev, the same information was told. Using Func solved my problem, however it's mysql specific, so I don't consider it a future proof solution.

I'll change it when 1.10 is out. Until then, I can use it this way.

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