简体   繁体   中英

SQL Server equivalent to Oracle LEAST?

I've been looking for a good equivalent to the Oracle LEAST function.

I'm hoping to implement a user defined function that does about 10 fairly complex calculations, and takes the minimum value from each of those calculations.

What I would do in Oracle is:

SELECT LEAST
(
select expression1 from dual,
select expression2 from dual,
select expression3 from dual
) from dual

See http://www.techonthenet.com/oracle/functions/least.php for more on Oracle LEAST.

If expression1 returned 10, expression2 return 5, and expression3 reeturned 30, the whole expression would return 5.

Because this may be about 10-20 calculations, the CASE WHEN syntax is going to get unwieldy fast.

Although the code will be more readable if we break it up more, I thought it would be more efficient to do it within one database query. Let me know I'm incorrect on that point, please!

Ie, is a stored procedure with 20 simple queries significantly slower than a stored procedure with one query that references a lot of tables all in one query.

mayby this query could help:

 SELECT  min(c1)  
 from ( 
      select expression1 as c1  
      union all
      select expression2 as c1 
      union all 
      select expression3 as c1
 )

The function Least() is applied horizontally in Oracle (on a row level), while Min() is applied vertically over a column. The example in the question required Min().

Azure SQL DB and future SQL Server versions now have greatest/least implemented:

https://docs.microsoft.com/en-us/sql/t-sql/functions/logical-functions-greatest-transact-sql?view=sql-server-ver15

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM