SQL query to obtain a summation

0

There is some way to get the sum of a table called initial_number, by means of dates, to be more clear I want the year 2011 to show me the total initial_number of that year, the year 2012 show me the total of the initial_number and so on

SELECT granjas.nombre_granja,año_destete, SUM(numero_inicial) AS total FROM 
formulario_precebo 
INNER JOIN granjas ON granjas.id = formulario_precebo.granja_id WHERE 
granja_id = 9 AND año_destete BETWEEN '2011' AND '2017'
ORDER BY total DESC;

The formula I am implementing brings me the whole sum between this range of year, but I want you to show me the sum is for each year.

    
asked by Juan Esteban Yarce 22.02.2018 в 15:15
source

1 answer

0

If I have understood correctly, what you need is one group per year.

SELECT granja_id ,año_destete,SUM(numero_inicial) as Suma_Anual
FROM formulario_precebo 
INNER JOIN granjas 
  ON granjas.id = formulario_precebo.granja_id 
WHERE 
 año_destete BETWEEN '2011' AND '2017'
group by granja_id ,año_destete
    
answered by 22.02.2018 / 15:37
source