如何修改此t-sql查询以返回不同列名的最大值?

时间:2011-11-03 13:46:03

标签: sql-server-2008 tsql

我有以下查询:

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

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

现在返回的示例结果是:

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

其中100是Rate,其他值对应Mon - Sun,但在这种情况下我只想返回100和600.

3 个答案:

答案 0 :(得分:8)

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

答案 1 :(得分:1)

您可以使用类似:

的详细信息
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   

很多打字,但这是一个可能的答案。您还可以执行以下操作,这种输入稍微少一些:

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   

当然,正确的解决方案将规范化您的数据库并使之成为现实,因为简单的连接和聚合就足够了。

答案 2 :(得分:1)

您可以使用union子查询来取消日期:

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