SQL MAX of multiple columns?

后端 未结 22 1808
伪装坚强ぢ
伪装坚强ぢ 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:27

    Scalar Function cause all sorts of performance issues, so its better to wrap the logic into an Inline Table Valued Function if possible. This is the function I used to replace some User Defined Functions which selected the Min/Max dates from a list of upto ten dates. When tested on my dataset of 1 Million rows the Scalar Function took over 15 minutes before I killed the query the Inline TVF took 1 minute which is the same amount of time as selecting the resultset into a temporary table. To use this call the function from either a subquery in the the SELECT or a CROSS APPLY.

    CREATE FUNCTION dbo.Get_Min_Max_Date
    (
        @Date1  datetime,
        @Date2  datetime,
        @Date3  datetime,
        @Date4  datetime,
        @Date5  datetime,
        @Date6  datetime,
        @Date7  datetime,
        @Date8  datetime,
        @Date9  datetime,
        @Date10 datetime
    )
    RETURNS TABLE
    AS
    RETURN
    (
        SELECT      Max(DateValue)  Max_Date,
                    Min(DateValue)  Min_Date
        FROM        (
                        VALUES  (@Date1),
                                (@Date2),
                                (@Date3),
                                (@Date4),
                                (@Date5),
                                (@Date6),
                                (@Date7),
                                (@Date8),
                                (@Date9),
                                (@Date10)
                    )   AS Dates(DateValue)
    )
    
    0 讨论(0)
  • 2020-11-22 02:27

    If you are using SQL Server 2005, you can use the UNPIVOT feature. Here is a complete example:

    create table dates 
    (
      number int,
      date1 datetime,
      date2 datetime,
      date3 datetime 
    )
    
    insert into dates values (1, '1/1/2008', '2/4/2008', '3/1/2008')
    insert into dates values (1, '1/2/2008', '2/3/2008', '3/3/2008')
    insert into dates values (1, '1/3/2008', '2/2/2008', '3/2/2008')
    insert into dates values (1, '1/4/2008', '2/1/2008', '3/4/2008')
    
    select max(dateMaxes)
    from (
      select 
        (select max(date1) from dates) date1max, 
        (select max(date2) from dates) date2max,
        (select max(date3) from dates) date3max
    ) myTable
    unpivot (dateMaxes For fieldName In (date1max, date2max, date3max)) as tblPivot
    
    drop table dates
    
    0 讨论(0)
  • 2020-11-22 02:27

    Based on the ScottPletcher's solution from http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/Q_24204894.html I’ve created a set of functions (e.g. GetMaxOfDates3 , GetMaxOfDates13 )to find max of up to 13 Date values using UNION ALL. See T-SQL function to Get Maximum of values from the same row However I haven't considered UNPIVOT solution at the time of writing these functions

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

    Well, you can use the CASE statement:

    SELECT
        CASE
            WHEN Date1 >= Date2 AND Date1 >= Date3 THEN Date1
            WHEN Date2 >= Date1 AND Date2 >= Date3 THEN Date2
            WHEN Date3 >= Date1 AND Date3 >= Date2 THEN Date3
            ELSE                                        Date1
        END AS MostRecentDate
    

    [For Microsoft SQL Server 2008 and above, you may consider Sven's simpler answer below.]

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

    Either of the two samples below will work:

    SELECT  MAX(date_columns) AS max_date
    FROM    ( (SELECT   date1 AS date_columns
               FROM     data_table         )
              UNION
              ( SELECT  date2 AS date_columns
                FROM    data_table
              )
              UNION
              ( SELECT  date3 AS date_columns
                FROM    data_table
              )
            ) AS date_query
    

    The second is an add-on to lassevk's answer.

    SELECT  MAX(MostRecentDate)
    FROM    ( SELECT    CASE WHEN date1 >= date2
                                  AND date1 >= date3 THEN date1
                             WHEN date2 >= date1
                                  AND date2 >= date3 THEN date2
                             WHEN date3 >= date1
                                  AND date3 >= date2 THEN date3
                             ELSE date1
                        END AS MostRecentDate
              FROM      data_table
            ) AS date_query 
    
    0 讨论(0)
  • 2020-11-22 02:29

    Using CROSS APPLY (for 2005+) ....

    SELECT MostRecentDate 
    FROM SourceTable
        CROSS APPLY (SELECT MAX(d) MostRecentDate FROM (VALUES (Date1), (Date2), (Date3)) AS a(d)) md
    
    0 讨论(0)
提交回复
热议问题