2
I’m looking for a percentage calculation. Taking the value that was received in that selected month (received = 1), divide by the total value of that selected month, and multiply by 100 to return the percentage. The logic I used below is working perfectly:
SET @total = (SELECT SUM(valor_receita) FROM receitas WHERE YEAR(data_vencimento) <= '2017' AND MONTH(data_vencimento) <= '06' AND id_usuario = 1);
SELECT SUM(valor_receita) AS valor_receita, ((SUM(valor_receita)/@total)*100) AS total
FROM receitas
WHERE recebido = 1 AND YEAR(data_vencimento) <= '2017' AND MONTH(data_vencimento) <= '06' AND id_usuario = 1;
What I would like to know is if there is any way to simplify this query by doing a JOIN or something like... without the need to also repeat twice the month and year.
Good!! It worked!! But it takes away a doubt.. in case I want to add there one that returns me the total amount of revenue, how could it be done?
– Eduardo Henrique
Then you have to repeat subselect, out of calculus, for it to be returned as a column. You can further decrease the code by taking this year and month comparison for a truncated date comparison
– Rovann Linhalis
"You can further decrease the code by taking this year and month comparison for a truncated date comparison" I have no idea how to do this... It was hard to think of this way... is that I’m using an input Month, q returns me '2017-06' for example.
– Eduardo Henrique
I edited adding another query, please do a test
– Rovann Linhalis
Thanks man, you’re 10!! It worked right here!
– Eduardo Henrique
=] do not forget to mark as reply. Thank you
– Rovann Linhalis