1

I found this on here by splattne, but couldn't post a comment asking the following question, so here it is.

-- the following expression calculates ==> max(@val1, @val2)
SELECT 0.5 * ((@val1 + @val2) + ABS(@val1 - @val2)) 

What would this formula look like to get the minimum?

What would this formula look like for 4 columns? (I know, more than one question.)

Community
  • 1
  • 1

1 Answers1

1
-- the following expression calculates ==> min(@val1, @val2)
SELECT 0.5 * ((@val1 + @val2) - ABS(@val1 - @val2))

for multi column, you might just want to create a function. depending on if you have fixed number of columns or variable number of columns to find minimum, you will have to create different approach for creating such function.

user3754372
  • 177
  • 16
  • I just have a fixed number of columns (4) that I am dealing with, but lots of records, so I don't really want to use a function for performance reasons. – OneMoreStep Sep 23 '14 at 18:30
  • ok then you will be putting same code you put in your function in the query. dont think it will be any performance problem for scalar function. i am assuming you know how to check for max of for values. a case statement should do. – user3754372 Sep 23 '14 at 18:36
  • you can also use [link](http://stackoverflow.com/a/6871572/3754372) to get min but it may be slower then case statements. – user3754372 Sep 23 '14 at 18:41