SQL Server 相当于 Oracle LEAST?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1972051/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 19:44:50  来源:igfitidea点击:

SQL Server equivalent to Oracle LEAST?

sql-serveroracletsql

提问by Jade

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

我一直在寻找与 Oracle LEAST 函数相当的等效项。

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.

我希望实现一个用户定义的函数,该函数执行大约 10 次相当复杂的计算,并从每个计算中获取最小值。

What I would do in Oracle is:

我会在 Oracle 中做的是:

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

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

有关Oracle LEAST 的更多信息,请参见http://www.techonthenet.com/oracle/functions/least.php

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

如果表达式 1 返回 10,表达式 2 返回 5,表达式 3 返回 30,则整个表达式将返回 5。

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

因为这可能需要大约 10-20 次计算,所以 CASE WHEN 语法很快就会变得笨拙。

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!

尽管如果我们将代码分解得更多,代码将更具可读性,但我认为在一个数据库查询中执行它会更有效。请让我知道我在这一点上是不正确的!

I.e., 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.

即,是一个具有 20 个简单查询的存储过程,比具有一个查询的存储过程慢得多,该查询在一个查询中引用了很多表。

回答by afftee

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
 )

回答by Stanislav Voynovski

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().

函数 Least() 在 Oracle 中水平应用(在行级别),而 Min() 垂直应用在列上。问题中的示例需要 Min()。