Wednesday, March 29, 2017

Re: From SQL to Django Queryset API

Hello Mauro,

The following should do

from django.db.models import F

Movimento.objects.values(
    'anno','impianto'
).annotate(
    dare=Sum('dare),
    avere=Sum('avere),
    diff=Sum(F('dare') - F('avere)),
)

Cheers,
Simon

Le mercredi 29 mars 2017 16:19:37 UTC-4, Mauro Ziliani a écrit :
Hi all.
My name's Mauro and I working on a DB (SQLite3 now and Postgres in the future).

I have a table Movimento with the fields
anno integer,impianto integer,dare decimal(10,2),avere decimal(10,2)

BY hands I can run

SELECT anno,impianto, sum(dare), sum(avere) FROM movimento 
GROUP BY anno,impianto
ORDER BY anno,impianto 

With django Api I write

Movimento.objects.values('anno','impianto').annotate(dare=Sum('dare), avere=Sum('avere))

And I get the same beaviour.

Now I need to translate the following SQL script

SELECT anno,impianto, sum(dare), sum(avere), sum(dare-avere)
FROM movimento 
GROUP BY anno,impianto
ORDER BY anno,impianto 

into django API queryset.

Is it possible todo this in one row?

Cna you give me some idea to solve this translaion?

Finally I'll render the results into a table.

Best regards,
   MZ

--
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 django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
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/9c8f43e7-6d84-4190-9dbc-beb25b3e1a95%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment