SQL MAX of multiple columns?

后端 未结 22 1801
伪装坚强ぢ
伪装坚强ぢ 2020-11-22 02:03

How do you return 1 value per row of the max of several columns:

TableName

[Number, Date1, Date2, Date3, Cost]

I n

相关标签:
22条回答
  • 2020-11-22 02:17

    Unfortunately Lasse's answer, though seemingly obvious, has a crucial flaw. It cannot handle NULL values. Any single NULL value results in Date1 being returned. Unfortunately any attempt to fix that problem tends to get extremely messy and doesn't scale to 4 or more values very nicely.

    databyss's first answer looked (and is) good. However, it wasn't clear whether the answer would easily extrapolate to 3 values from a multi-table join instead of the simpler 3 values from a single table. I wanted to avoid turning such a query into a sub-query just to get the max of 3 columns, also I was pretty sure databyss's excellent idea could be cleaned up a bit.

    So without further ado, here's my solution (derived from databyss's idea).
    It uses cross-joins selecting constants to simulate the effect of a multi-table join. The important thing to note is that all the necessary aliases carry through correctly (which is not always the case) and this keeps the pattern quite simple and fairly scalable through additional columns.

    DECLARE @v1 INT ,
            @v2 INT ,
            @v3 INT
    --SET @v1 = 1 --Comment out SET statements to experiment with 
                  --various combinations of NULL values
    SET @v2 = 2
    SET @v3 = 3
    
    SELECT  ( SELECT    MAX(Vals)
              FROM      ( SELECT    v1 AS Vals
                          UNION
                          SELECT    v2
                          UNION
                          SELECT    v3
                        ) tmp
              WHERE     Vals IS NOT NULL -- This eliminates NULL warning
    
            ) AS MaxVal
    FROM    ( SELECT    @v1 AS v1
            ) t1
            CROSS JOIN ( SELECT @v2 AS v2
                       ) t2
            CROSS JOIN ( SELECT @v3 AS v3
                       ) t3
    
    0 讨论(0)
  • 2020-11-22 02:19
    SELECT 
        CASE 
            WHEN Date1 >= Date2 AND Date1 >= Date3 THEN Date1 
            WHEN Date2 >= Date3 THEN Date2 
            ELSE Date3
        END AS MostRecentDate 
    

    This is slightly easier to write out and skips evaluation steps as the case statement is evaluated in order.

    0 讨论(0)
  • 2020-11-22 02:20

    If you're using MySQL, you can use

    SELECT GREATEST(col1, col2 ...) FROM table
    
    0 讨论(0)
  • 2020-11-22 02:23

    Above table is an employee salary table with salary1,salary2,salary3,salary4 as columns.Query below will return the max value out of four columns

    select  
     (select Max(salval) from( values (max(salary1)),(max(salary2)),(max(salary3)),(max(Salary4)))alias(salval)) as largest_val
     from EmployeeSalary
    

    Running above query will give output as largest_val(10001)

    Logic of above query is as below:

    select Max(salvalue) from(values (10001),(5098),(6070),(7500))alias(salvalue)
    

    output will be 10001

    0 讨论(0)
  • 2020-11-22 02:25

    Another way to use CASE WHEN

    SELECT CASE true 
           WHEN max(row1) >= max(row2) THEN CASE true WHEN max(row1) >= max(row3) THEN max(row1) ELSE max(row3) end ELSE
           CASE true WHEN max(row2) >= max(row3) THEN max(row2) ELSE max(row3) END END
    FROM yourTable
    
    0 讨论(0)
  • 2020-11-22 02:25

    here is a good solution:

    CREATE function [dbo].[inLineMax] (@v1 float,@v2 float,@v3 float,@v4 float)
    returns float
    as
    begin
    declare @val float
    set @val = 0 
    declare @TableVal table
    (value float )
    insert into @TableVal select @v1
    insert into @TableVal select @v2
    insert into @TableVal select @v3
    insert into @TableVal select @v4
    
    select @val= max(value) from @TableVal
    
    return @val
    end 
    
    0 讨论(0)
提交回复
热议问题