Error - Select with SUM

Asked

Viewed 45 times

0

Expensive;

I have a table with several information, among them a field with value_boleto and another with maturity.

It needed first to be ordered the compo due with the current month (current) and after that ordered, add the values of value_boleto.

I tried the query below, but without success:

SELECT * FROM boleto WHERE extract(year_month from vencimento) = 201805, (SUM(-valor_boleto)+5000) AS valor_boleto FROM boleto";

The error generated is syntax from the SUM.

If I only execute the ordering of the current month, it works correctly:

SELECT * FROM boleto WHERE extract(year_month from vencimento) = 201805;

Where I am missing, or rather has a simple frma to make this query?

  • Friend, despite some errors in your sql, I think I understand what you are wanting. But let’s define a few things first. You need to select all columns of your table or just want the sum of values grouped by period?

1 answer

1


Friend, if I understand what you want, I believe that grouping by the period you get the sum of the values of the boletos as you want, follows the code:

SELECT (SUM(valor_boleto)+5000) as soma, extract(year_month from vencimento) as periodo FROM boleto 
GROUP BY extract(year_month from vencimento);

follow the example: Sqlfiddle

  • Good evening @Inacio. Almost that my friend, returned the sum of all the months. How do I set the current month. I tried to set in hand Extract(year_month from expiration) = '201805' and the expiration came all empty, only the sum returned result.

  • Friend, I decided. I had to set at the end of the code the month I want. I will give as accepted answer. Hugs.

Browser other questions tagged

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