SQL Server 2008中的函数类似于mysql中的GREATEST?

时间:2011-01-18 15:22:41

标签: sql sql-server sql-server-2008

我想找到多列的最大值。

MySQL支持GREATEST功能,但SQL Server不支持。

SQL Server 2008中是否有与此类似的功能?

5 个答案:

答案 0 :(得分:21)

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

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

或者比2008年更简洁的版本。

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

答案 1 :(得分:5)

为此,我创建了一个标量函数,如下所示:

CREATE FUNCTION [dbo].[MaxOrNull](@val1 int, @val2 int)
returns int
as
begin
    if @val1 >= @val2 RETURN @val1
    if @val1 < @val2 RETURN @val2

    RETURN NULL
end

这是最优雅的解决方案,可以在SQL代码中的任何位置使用。

答案 2 :(得分:2)

尝试使用TOP代替MAX

答案 3 :(得分:2)

我会推荐以下解决方案:

SELECT (CASE WHEN t.createdt < t.changedt THEN t.changedt ELSE t.created END) AS ChgDate
  FROM table t

答案 4 :(得分:1)

可能的解决方案:

Create FUNCTION [dbo].[MaxOf]
    (
      @val1 INT =0,
      @val2 INT=0 ,
      @val3 INT =0,
      @val4 INT =0,
      @val5 INT =0,
      @val6 INT =0,
      @val7 INT =0,
      @val8 INT =0,
      @val9 INT =0,
      @val10 INT =0,
      @val11 INT =0,
      @val12 INT =0,
      @val13 INT =0,
      @val14 INT =0,
      @val15 INT =0,
      @val16 INT =0,
      @val17 INT =0,
      @val18 INT =0,
      @val19 INT =0,
      @val20 INT  =0)
      --OUTPUT 
     RETURNS INT  WITH SCHEMABINDING
AS  
   BEGIN
        DECLARE  @MAX AS INT ;
        SET @MAX=0
        IF isnull(@val1,0)> isnull(@MAX,0) SET @MAX=isnull(@val1,0) 
        IF isnull(@val2,0)> isnull(@MAX,0) SET @MAX=isnull(@val2,0) 
        IF isnull(@val3,0)> isnull(@MAX,0) SET @MAX=isnull(@val3,0) 
        IF isnull(@val4,0)> isnull(@MAX,0) SET @MAX=isnull(@val4,0) 
        IF isnull(@val5,0)> isnull(@MAX,0) SET @MAX=isnull(@val5,0) 
        IF isnull(@val6,0)> isnull(@MAX,0) SET @MAX=isnull(@val6,0) 
        IF isnull(@val7,0)> isnull(@MAX,0) SET @MAX=isnull(@val7,0) 
        IF isnull(@val8,0)> isnull(@MAX,0) SET @MAX=isnull(@val8,0) 
        IF isnull(@val9,0)> isnull(@MAX,0) SET @MAX=isnull(@val9,0) 
        IF isnull(@val10,0)> isnull(@MAX,0) SET @MAX=isnull(@val10,0) 
        IF isnull(@val11,0)> isnull(@MAX,0) SET @MAX=isnull(@val11,0) 
        IF isnull(@val12,0)> isnull(@MAX,0) SET @MAX=isnull(@val12,0) 
        IF isnull(@val13,0)> isnull(@MAX,0) SET @MAX=isnull(@val13,0) 
        IF isnull(@val14,0)> isnull(@MAX,0) SET @MAX=isnull(@val14,0) 
        IF isnull(@val15,0)> isnull(@MAX,0) SET @MAX=isnull(@val15,0) 
        IF isnull(@val16,0)> isnull(@MAX,0) SET @MAX=isnull(@val16,0) 
        IF isnull(@val17,0)> isnull(@MAX,0) SET @MAX=isnull(@val17,0) 
        IF isnull(@val18,0)> isnull(@MAX,0) SET @MAX=isnull(@val18,0) 
        IF isnull(@val19,0)> isnull(@MAX,0) SET @MAX=isnull(@val19,0) 
        IF isnull(@val20,0)> isnull(@MAX,0) SET @MAX=isnull(@val20,0) 

        RETURN @MAX ;
    END

,电话会是

SELECT dbo.MaxOf (2,3,4,0,0,0,0,200,8,0,0,0,0,0,0,0,0,0,0,0)