SQL Server 2008 中的功能类似于 mysql 中的 GREATEST? [英] Function in SQL Server 2008 similar to GREATEST in mysql?

查看:33
本文介绍了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

这篇关于SQL Server 2008 中的功能类似于 mysql 中的 GREATEST?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆