[Fixed]-Django GROUP BY strftime date format

14👍

This works for me:

select_data = {"d": """strftime('%%m/%%d/%%Y', time_stamp)"""}

data = My_Model.objects.extra(select=select_data).values('d').annotate(Sum("numbers_data")).order_by()

Took a bit to figure out I had to escape the % signs.

5👍

As of v1.8, you can use Func() expressions.

For example, if you happen to be targeting AWS Redshift’s date and time functions:

from django.db.models import F, Func, Value

def TimezoneConvertedDateF(field_name, tz_name):
    tz_fn = Func(Value(tz_name), F(field_name), function='CONVERT_TIMEZONE')
    dt_fn = Func(tz_fn, function='TRUNC')
    return dt_fn

Then you can use it like this:

SomeDbModel.objects \
 .annotate(the_date=TimezoneConvertedDateF('some_timestamp_col_name',
                                           'America/New_York')) \
 .filter(the_date=...)

or like this:

SomeDbModel.objects \
 .annotate(the_date=TimezoneConvertedDateF('some_timestamp_col_name',
                                           'America/New_York')) \
 .values('the_date') \
 .annotate(...)

1👍

Any reason not to just do this in the database, by running the following query against the database:

select date, sum(numbers_data) 
from my_model 
group by date;

If your answer is, the date is a datetime with non-zero hours, minutes, seconds, or milliseconds, my answer is to use a date function to truncate the datetime, but I can’t tell you exactly what that is without knowing what RBDMS you’re using.

👤tpdi

1👍

I’m not sure about strftime, my solution below is using sql postgres trunc…

select_data = {"date": "date_trunc('day', creationtime)"}       
ttl = ReportWebclick.objects.using('cms')\
                    .extra(select=select_data)\
                    .filter(**filters)\
                    .values('date', 'tone_name', 'singer', 'parthner', 'price', 'period')\
                    .annotate(loadcount=Sum('loadcount'), buycount=Sum('buycount'), cancelcount=Sum('cancelcount'))\
                    .order_by('date', 'parthner')

— equal to sql query execution:

select date_trunc('month', creationtime) as date, tone_name, sum(loadcount), sum(buycount), sum(cancelcount)
from webclickstat
group by tone_name, date;

0👍

my solution like this when my db is mysql:

select_data = {"date":"""FROM_UNIXTIME( action_time,'%%Y-%%m-%%d')"""} 
qs = ViewLogs.objects.filter().extra(select=select_data).values('mall_id', 'date').annotate(pv=Count('id'), uv=Count('visitor_id', distinct=True))

to use which function, you can read mysql datetime processor docs like DATE_FORMAT,FROM_UNIXTIME…

👤Steven

Leave a comment