Apologies. The code I posted is actually correct and well functioning. 
The reason that the results looked wrong was that the data handled by the 
code was faulty.
In my research, I found another way to do the trick:

    contracts = (Project.objects.values('stage')
                                .annotate(contract_sum=Sum('contractSum'))
                                .annotate(expected_own_production_sum=Sum(
'id', field='contractSum * probability'))
                                )

cheers, Mikkel



lørdag den 4. august 2018 kl. 14.11.27 UTC+2 skrev Mikkel Kromann:
>
> Dear Django users.
>
> I'm trying to do the sums of the product of two columns ('contract_sum' 
> and 'probability', decimal fields), grouping by a third column, 'stage' (a 
> choice variable).
> I'm also reporting the grouped sums of one of the single columns 
> ('contract_sum')
> The single column works just fine, but I just can't get it right with the 
> product columns.
> Below is my code so far:
>
> contracts = (Project.objects.values('stage')
>                             .annotate(contract_sum=Sum('contractSum'))
>                             .annotate(expected_own_production_sum=F(
> 'contractSum')*F('probability'))
>                             )
>
> a) When I run the query with the single column query, the results are 
> right.
> b) When I run the query with both selections, not only is the column 
> product, wrong, but the single column sums also become wrong.
> c) When I run the query with only the product columns, the query returns 
> zero for all groups, which is definitely wrong.
>
> Can I do this right without going to raw sql?
>
>
> cheers + thanks, Mikkel
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/a38df7fa-dc7b-4042-886f-4fa7a74f2c45%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to