Return lower value of a SUM?

1

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

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

The table sums all values for each vendor.

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

Example This search returned above

| supplier | Customer | TOTAL |

| 1 | 1

asked by anonymous 06.07.2017 / 15:27

1 answer

3

SUM function

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

MIN function

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

Return lowest value

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

Your code looks like this:

SELECT distinct fornecedor, cliente, 
MIN(valor*qtd) AS TOTAL FROM orcamentos 
WHERE idOrcamento ='$orcamento' group by 
fornecedor, cliente;
    
06.07.2017 / 15:36