如何修改此 t-sql 查询以返回不同列名的最大值? [英] How to I modify this t-sql query to return the maximum value for different column names?

查看:27
本文介绍了如何修改此 t-sql 查询以返回不同列名的最大值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下查询:

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

我不想返回所有列,我只想返回 Rate 和 RateMon、RateTue、RateWed、RateThu、RateFri、RateSat 和 RateSun 之间的最大值,但我很难因为列名不同.

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

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

其中 100 是 Rate,其他值对应于 Mon - Sun,但在这种情况下我只想返回 100 和 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.

推荐答案

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

这篇关于如何修改此 t-sql 查询以返回不同列名的最大值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆