原生SQL
SELECT order_id,city,locality,login_time,sum(morning_hours),sum(afternoon_hours),sum(evening_hours),sum(total_hours)
FROM orders
GROUPBY order_id,city,locality,login_time`
group by … sum
from django.db.models import Sum
Your_Model.objects.values(
"order_id", "city", "locality", "login_time"
).order_by().annotate(
Sum("morning_hours"),
Sum("afternoon_hours"),
Sum("evening_hours"),
Sum("total_hours"),
)
group by …count
from django.db.models import Count
result = Books.objects.values('author')
.order_by('author')
.annotate(count=Count('author'))
https://docs.djangoproject.com/en/4.2/topics/db/aggregation/