Answers for "Group by date (day, month, year)"

0

Group by date (day, month, year)

from django.db.models.functions import TruncMonth
from django.db.models import Count

Sales.objects
    .annotate(month=TruncMonth('created'))  # Truncate to month and add to select list
    .values('month')                          # Group By month
    .annotate(c=Count('id'))                  # Select the count of the grouping
    .values('month', 'c')                     # (might be redundant, haven't tested) select month and count
Posted by: Guest on March-25-2022

Code answers related to "Group by date (day, month, year)"

Python Answers by Framework

Browse Popular Code Answers by Language