How to I modify this t-sql query to return the maximum value for different column names?

后端 未结 3 1867
感动是毒
感动是毒 2020-12-30 14:53

I have the following query:

SELECT
        [Rate],
        [RateMon],
        [RateTue],
        [RateWed],
        [RateThu],
        [RateFri],
        [Ra         


        
相关标签:
3条回答
  • 2020-12-30 15:15
    SELECT  [Rate],
            (SELECT MAX(T.[Rate])
             FROM (VALUES([RateMon]),
                         ([RateTue]),
                         ([RateWed]),
                         ([RateThu]),
                         ([RateFri]),
                         ([RateSat]),
                         ([RateSun])) AS T([Rate])
            ) AS MaxRate
    FROM [Room]
    WHERE Id=@Id
    
    0 讨论(0)
  • 2020-12-30 15:30

    You can use something verbose like:

    SELECT Rate,
      CASE 
        WHEN RateMon>=RateTue AND RateMon>=RateWed AND RateMon>=RateThu AND 
             RateMon>=RateFri AND RAteMon>=RateSat AND RateMon>=RateSun THEN RateMon
        WHEN RateTue>=RateMon AND RateTue>=RateWed AND RateTue>=RateThu AND 
             RateTue>=RateFri AND RateTue>=RateSat AND RateTue>=RateSun THEN RateTue
        WHEN RateWed>=RateMon AND RateWed>=RateTue AND RateWed>=RateThu AND 
             RateWed>=RateFri AND RateWed>=RateSat AND RateWed>=RateSun THEN RateWed
        WHEN RateThu>=RateMon AND RateThu>=RateTue AND RateThu>=RateWed AND 
             RateThu>=RateFri AND RateThu>=RateSat AND RateThu>=RateSun THEN RateThu
        WHEN RateFri>=RateMon AND RateFri>=RateTue AND RateFri>=RateWed AND 
             RateFri>=RateThu AND RateFri>=RateSat AND RateFri>=RateSun THEN RateFri
        WHEN RateSat>=RateMon AND RateSat>=RateTue AND RateSat>=RateWed AND 
             RateSat>=RateThu AND RateSat>=RateFri AND RateSat>=RateSun THEN RateSat
        WHEN RateSun>=RateMon AND RateSun>=RateTue AND RateSun>=RateWed AND 
             RateSun>=RateThu AND RateSun>=RateFri AND RateSun>=RateSat THEN RateSun
        END AS MaxRate 
    FROM    
        [Room]    
    WHERE    
        Id=@Id   
    

    A lot of typing, but that's one possible answer. You can also do the following, which is slightly less typing:

    SELECT Rate, MAX(Rates.Rate) AS MaxRate
      FROM    
        [Room], 
          (SELECT RateMon AS Rate FROM [Room] WHERE Id=@Id UNION
           SELECT RateTue AS Rate FROM [Room] WHERE Id=@Id UNION
           SELECT RateWed AS Rate FROM [Room] WHERE Id=@Id UNION
           SELECT RateThu AS Rate FROM [Room] WHERE Id=@Id UNION
           SELECT RateFri AS Rate FROM [Room] WHERE Id=@Id UNION
           SELECT RateSat AS Rate FROM [Room] WHERE Id=@Id UNION
           SELECT RateSun AS Rate FROM [Room] WHERE Id=@Id)
         AS Rates
      WHERE    
        Id=@Id   
    

    Of course, the correct solution would be to normalize your database and make this moot, since a simple join and aggregate would be sufficient.

    0 讨论(0)
  • 2020-12-30 15:33

    You could unpivot the days with a union subquery:

    select  Rate
    ,       max(DayRate)
    from    (
            select ID, Rate, RateMon as DayRate from Room
            union all
            select ID, Rate, RateTue from Room
            union all
            select ID, Rate, RateWed from Room
            union all
            ....
            ) as SubQuery
    where   ID = @ID
    group by
            Rate
    
    0 讨论(0)
提交回复
热议问题