Return lower value of a SUM?

Asked

Viewed 349 times

1

I have a SELECT with SUM and would like to understand how I do to return me the lowest sum of all value per supplier.

SELECT distinct fornecedor, cliente, SUM(valor*qtd) AS TOTAL FROM orcamentos 
WHERE idOrcamento ='$orcamento' group by fornecedor;

The Table sums all values of each supplier.

I need the result to show me the lowest value found among suppliers.

Example this search above returned

|supplier | Customer | TOTAL|

|1________| 1_____ | 10.00|

|2________| 1 _____ | 11.00|

|3________| 1 _____ | 10.50|

How do I make SELECT return only the line with lower value?

  • with min ..... that is to say, MIN(valor*qtd)

  • SELECT cliente, min(valor*qtd) from orcamentos WHERE idOrcamento =$orcamento group by cliente is the smallest per customer so ???

  • correct, based on the values of each supplier

1 answer

3

SUM function

The function SUM() returns the total sum of a numerical column.

MIN function

The function MIN() returns the smallest value of the selected column.

Return the lowest value

As you want to return the smallest value in sql function, you should use the function MIN() instead of using the function SUM() as @Virgilionovic already responded in his comment.

Your code would look like this:

SELECT distinct fornecedor, cliente, 
MIN(valor*qtd) AS TOTAL FROM orcamentos 
WHERE idOrcamento ='$orcamento' group by 
fornecedor, cliente;
  • Will this SELECT return the lowest value from each of the right suppliers/customers? I think what he wants is a SELECT that returns the supplier/customer with lower added value

  • 1

    @Joséhenriqueluckmann if this is his doubt then she was badly explained, it would be interesting for him to interact and edit the question to be clearer.

  • @Wendell thanks, I repeated the question to make it clearer, tenteni MIN(SUM()) but returned me error.

Browser other questions tagged

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