Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Minimum/Maximum function in T-SQL?

Tags:

sql

t-sql

I am not asking about the aggregate Min/Max functions here. I would like to know if there are functions to get the mix or max of two values as in:

SELECT Maximum(a,b)
FROM Foo

If table Foo contains

a b
1 2
4 3

Then the results should be 2, then 4.

I can do this with an IF or CASE statement, but you'd think there would be some simple math functions for this.

Thank you,

Daniel

like image 754
Daniel Williams Avatar asked Aug 16 '26 01:08

Daniel Williams


1 Answers

There is not. You can write your own UDFs but UDFs can slow queries down. Another option is to UNPIVOT the data so you can use the aggregate function. But for small applications CASE is best.

like image 187
jimconstable Avatar answered Aug 17 '26 17:08

jimconstable