Skip to content
Advertisement

SQL getting the minimum out of calculation of 3 values

I have multiple rows that each have 3 columns and what I want to do is calculate these 3 columns at each row and see which row has the lowest sum

for example:

row 1 - col1(1)+col2(3)+col3(0) = 4
row 2 - col1(50) + col2(3) + col3(4) = 57
row 3 - col1(2) + col2(0) + col3(0) = 2

the selected row (row 3) will show me the calculated value = 2

currently I have been calculating only by 1 column but now I need 3

this is what I was doing:

select min(Bonus) from dbo.Bonuses AS c1

my table (Bonuses)

ID      Bonus    Double Bonus   Master Bonus
1         1          3              0
2         50         3              4
3         2          0              0 

expected results:

the row 3 is the minimum in value will print

2

Advertisement

Answer

you can do it with:

select min(col1 + col2 + col3) from table;

col1 + col2 + col3 – calculate sum of every line (row) of the table;

min(col1 + col2 + col3) – determine minimal value. In your case:

select min("Bonus" + "Double Bonus" + "Master Bonus") from "Bonuses";
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement