Valeriu
Valeriu

Reputation: 41

Django database function to extract week from date (if week starts on Saturday)

    loads_of_dedicated_customers = Load.objects.filter(customer__is_dedicated=True).exclude(load_status='Cancelled').values('customer__name').annotate(year=ExtractYear('drop_date'), week=ExtractWeek('drop_date').annotate(
        loads_count=Count('id'),
        sum_revenue = Sum('billable_amount'),
        sum_miles = Sum('total_miles'),
        rate_per_mile = ExpressionWrapper(F('sum_revenue') / NullIf(F('sum_miles'), 0), output_field=DecimalField(max_digits=7, decimal_places=2)),
    )

I have this query that groups by year and week. However, I need the results by week where the week starts on Saturday. Is there a simple way to do this or should I create a week columns in the table and save it there?

Thanks!

Upvotes: 0

Views: 49

Answers (0)

Related Questions