Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have the following query:

SELECT
        [Rate],
        [RateMon],
        [RateTue],
        [RateWed],
        [RateThu],
        [RateFri],
        [RateSat],
        [RateSun]
    FROM
        [Room]
    WHERE
        Id=@Id

Instead of returning all the columns, I just want to return Rate and the Maximum value between RateMon, RateTue, RateWed, RateThu, RateFri, RateSat, and RateSun, but I am having a hard time because the column names are different.

An example result return now is:

100, 400, 400, 400, 400, 600, 600, 600

where 100 is the Rate and the other values correspond to Mon - Sun, but I want to return just 100 and 600 in this case.

like image 652
Xaisoft Avatar asked Nov 03 '11 13:11

Xaisoft


1 Answers

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
like image 114
Mikael Eriksson Avatar answered Nov 15 '22 12:11

Mikael Eriksson