I need to recover the sum of the highest values of each month

Asked

Viewed 27 times

-1

SELECT SUM(valor_parametros) FROM (SELECT valor_parametros,cnpj,data_movimento FROM (SELECT valor_parametros,cnpj,data_movimento,id FROM  movimento GROUP BY MONTH(data_movimento) ORDER BY id DESC)  movimento where YEAR(data_movimento)='2020' ORDER BY id DESC) AS T GROUP BY cnpj

for example: cnpj 13566400130 would have to return me 10.2 of the day 2020-05-08 ,but the value of 10.20 of 2020-04-06 would have to return 20.40

inserir a descrição da imagem aqui

1 answer

0

To search the values grouped by month and CNPJ and have the sum of the values, as you commented in your example and in the image, you can use the clause group by next to the sum.

For example, the query below would return the month (extracted from the data_movement), the CNPJ and the sum of the column value_parameters, grouped by the month itself and CNPJ. This way, you will have for each month each CNPJ and the sum of value_parameters.

SELECT MONTH(data_movimento), cnpj, SUM(valor_parametros) 
FROM movimento
GROUP BY MONTH(data_movimento), cnpj

In case this isn’t exactly your question, I’ll need more details to help you!

Browser other questions tagged

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