Thursday, January 29, 2015

sql query to annotation values e aggretate statements

Hi all.
My name's Mauro and I start to work with Django 1.7.3
I work on my project which tracks my accounting.

I need to translate a SQL query to annoteta values aggretate statements.

The application is named accounting

The models involved are:


class Nominativo(models.Model):
  nome = models.CharField(max_length=100)
  
class Impianto(models.Model):
  nome = models.CharField(max_length=100)

class Capitolo(models.Model):
  nome = models.CharField(max_length=100)
  

class Registrazione(models.Model):
  anno = models.PositiveIntegerField()
  impianto = models.ForeignKey('Impianto')
  nominativo = models.ForeignKey('Nominativo')
  capitolo = models.ForeignKey('Capitolo')

  dare = models.DecimalField(max_digits=10, decimal_places=2)
  avere = models.DecimalField(max_digita=10, decimal_places=2)


I need to translate te following sql statement

SELECT capitolo.nome, sum(dare) as uscite, sum(avere) as entrate 
FROM contabilita_registrazione 
INNER JOIN contabilita_capitolo as capitolo ON contabilita_registrazione.capitolo_id=capitolo.id 
WHERE anno=2014 
GROUP BY capitolo.nome 
ORDER BYcapitolo.nome;

How can obtain the same behavior using ORM?

Thanks in advance

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 http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/2b6998bf-741e-46c1-8721-e1870e5fe608%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment