SQL Server 2008 中的功能类似于 mysql 中的 GREATEST?

问题描述:

我想找到多列的最大值.

I want to find the maximum value of multiple columns.

MySQL 支持 GREATEST 功能,但 SQL Server 没有.

MySQL supports the GREATEST function but SQL Server doesn't.

SQL Server 2008 有没有类似的功能?

Is there any function similar to this in SQL Server 2008?

没有.但是子查询可以访问外部查询中的列,因此您可以添加子查询 UNION ALL 将感兴趣的列作为派生表,然后从中选择 max.

No. But a sub query can access the columns from the outer query so you can add a sub query UNION ALL ing the columns of interest as a derived table then select the max from that.

SELECT *, 
      (SELECT MAX(c) FROM 
                    (SELECT number AS c 
                     UNION ALL 
                     SELECT status) T) AS GreatestNumberOrStatus
FROM master..spt_values

或者像 2008 年那样更简洁的版本.

Or a slightly more concise version as you are on 2008.

SELECT *, 
      (SELECT MAX(c) FROM (VALUES(number),(status)) T (c)) AS Greatest
FROM master..spt_values