SQL MAX of multiple columns?

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

    For T-SQL (MSSQL 2008+)

    SELECT
      (SELECT
         MAX(MyMaxName) 
       FROM ( VALUES 
                (MAX(Field1)), 
                (MAX(Field2)) 
            ) MyAlias(MyMaxName)
      ) 
    FROM MyTable1
    
    0 讨论(0)
  • 2020-11-22 02:10

    Here is another nice solution for the Max functionality using T-SQL and SQL Server

    SELECT [Other Fields],
      (SELECT Max(v) 
       FROM (VALUES (date1), (date2), (date3),...) AS value(v)) as [MaxDate]
    FROM [YourTableName]
    
    0 讨论(0)
  • 2020-11-22 02:11

    Problem: choose the minimum rate value given to an entity Requirements: Agency rates can be null

    [MinRateValue] = 
    CASE 
       WHEN ISNULL(FitchRating.RatingValue, 100) < = ISNULL(MoodyRating.RatingValue, 99) 
       AND  ISNULL(FitchRating.RatingValue, 100) < = ISNULL(StandardPoorsRating.RatingValue, 99) 
       THEN FitchgAgency.RatingAgencyName
    
       WHEN ISNULL(MoodyRating.RatingValue, 100) < = ISNULL(StandardPoorsRating.RatingValue , 99)
       THEN MoodyAgency.RatingAgencyName
    
       ELSE ISNULL(StandardPoorsRating.RatingValue, 'N/A') 
    END 
    

    Inspired by this answer from Nat

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

    Please try using UNPIVOT:

    SELECT MAX(MaxDt) MaxDt
       FROM tbl 
    UNPIVOT
       (MaxDt FOR E IN 
          (Date1, Date2, Date3)
    )AS unpvt;
    
    0 讨论(0)
  • 2020-11-22 02:15

    There are 3 more methods where UNPIVOT (1) is the fastest by far, followed by Simulated Unpivot (3) which is much slower than (1) but still faster than (2)

    CREATE TABLE dates
        (
          number INT PRIMARY KEY ,
          date1 DATETIME ,
          date2 DATETIME ,
          date3 DATETIME ,
          cost INT
        )
    
    INSERT  INTO dates
    VALUES  ( 1, '1/1/2008', '2/4/2008', '3/1/2008', 10 )
    INSERT  INTO dates
    VALUES  ( 2, '1/2/2008', '2/3/2008', '3/3/2008', 20 )
    INSERT  INTO dates
    VALUES  ( 3, '1/3/2008', '2/2/2008', '3/2/2008', 30 )
    INSERT  INTO dates
    VALUES  ( 4, '1/4/2008', '2/1/2008', '3/4/2008', 40 )
    GO
    

    Solution 1 (UNPIVOT)

    SELECT  number ,
            MAX(dDate) maxDate ,
            cost
    FROM    dates UNPIVOT ( dDate FOR nDate IN ( Date1, Date2,
                                                Date3 ) ) as u
    GROUP BY number ,
            cost 
    GO
    

    Solution 2 (Sub query per row)

    SELECT  number ,
            ( SELECT    MAX(dDate) maxDate
              FROM      ( SELECT    d.date1 AS dDate
                          UNION
                          SELECT    d.date2
                          UNION
                          SELECT    d.date3
                        ) a
            ) MaxDate ,
            Cost
    FROM    dates d
    GO
    

    Solution 3 (Simulated UNPIVOT)

    ;WITH    maxD
              AS ( SELECT   number ,
                            MAX(CASE rn
                                  WHEN 1 THEN Date1
                                  WHEN 2 THEN date2
                                  ELSE date3
                                END) AS maxDate
                   FROM     dates a
                            CROSS JOIN ( SELECT 1 AS rn
                                         UNION
                                         SELECT 2
                                         UNION
                                         SELECT 3
                                       ) b
                   GROUP BY Number
                 )
        SELECT  dates.number ,
                maxD.maxDate ,
                dates.cost
        FROM    dates
                INNER JOIN MaxD ON dates.number = maxD.number
    GO
    
    DROP TABLE dates
    GO
    
    0 讨论(0)
  • 2020-11-22 02:17
    DECLARE @TableName TABLE (Number INT, Date1 DATETIME, Date2 DATETIME, Date3 DATETIME, Cost MONEY)
    
    INSERT INTO @TableName 
    SELECT 1, '20000101', '20010101','20020101',100 UNION ALL
    SELECT 2, '20000101', '19900101','19980101',99 
    
    SELECT Number,
           Cost  ,
           (SELECT MAX([Date])
           FROM    (SELECT Date1 AS [Date]
                   UNION ALL
                   SELECT Date2
                   UNION ALL
                   SELECT Date3
                   )
                   D
           )
           [Most Recent Date]
    FROM   @TableName
    
    0 讨论(0)
提交回复
热议问题