Add lines with the same year

Asked

Viewed 60 times

0

Problem:-Add lines that have equal years via mysql command.

Query I am using:

SELECT produtividade, ano
FROM area_e_producao_05_15
INNER JOIN dados_cidades
ON edr_id={$edr['edr_id']}
WHERE area_e_producao_05_15.id_municipio=dados_cidades.dados_cidades_id AND Ano BETWEEN {$ano} and {$ano2} Order By Ano

My result was:

inserir a descrição da imagem aqui

In this case the result was little, but in some cases are generated many equal years. Is there any way to add these productivity and present a line with each year? Photo example the expected result is:

productivity|Year 50 |2010 50 |2011

  • uses a group by year

  • You can use GROUP BY;

  • SELECT SUM(productivity) Productivity, year.... WHERE .... GROUP BY year;

  • Group By it does not perform the sum. @ramaral but this is another query? or is saying to join what is there?

  • This is your query with SUM and GROUP BY added.

1 answer

2


Try this:

 SELECT sum(produtividade), ano
   FROM area_e_producao_05_15
  INNER JOIN dados_cidades
     ON edr_id={$edr['edr_id']}
  WHERE area_e_producao_05_15.id_municipio=dados_cidades.dados_cidades_id 
    AND Ano BETWEEN {$ano} and {$ano2} 
  GROUP BY ano
  ORDER BY ano

Browser other questions tagged

You are not signed in. Login or sign up in order to post.