选择每小时的值并在单独的列中突出显示最大值

时间:2014-04-16 22:51:10

标签: mysql

我正在运行以下查询..

select date_format(timestamp, '%Y-%m-%d %H:00:00') as hour
     , current_value
     , "null" as null_value
  from History_Data 
 where (timestamp >= (now() - interval 24 hour)) 
    and metric_id = '4'
  group by hour  
  order by hour desc
  limit 24

及其归来..

 hour   current_value   null_value
 2014-04-16 23:00:00    41449   null
 2014-04-16 22:00:00    117462  null
 2014-04-16 21:00:00    103954  null
 2014-04-16 20:00:00    39421   null
 2014-04-16 19:00:00    17269   null
 2014-04-16 18:00:00    14950   null
 2014-04-16 17:00:00    13813   null
 2014-04-16 16:00:00    13246   null
 2014-04-16 15:00:00    12882   null
 2014-04-16 14:00:00    13512   null
 2014-04-16 13:00:00    13228   null
 2014-04-16 12:00:00    12850   null
 2014-04-16 11:00:00    12918   null
 2014-04-16 10:00:00    11923   null
 2014-04-16 09:00:00    11480   null
 2014-04-16 08:00:00    10890   null
 2014-04-16 07:00:00    10285   null
 2014-04-16 06:00:00    10350   null
 2014-04-16 05:00:00    10151   null
 2014-04-16 04:00:00    10917   null
 2014-04-16 03:00:00    12714   null
 2014-04-16 02:00:00    14758   null
 2014-04-16 01:00:00    18018   null
 2014-04-16 00:00:00    21584   null

我几乎就在那里..我想要的是突出显示最大值..所以输出应该看起来像..

 hour            current_value  null_value
 2014-04-16 23:00:00    41449   null
 2014-04-16 22:00:00    117462  null
 **2014-04-16 21:00:00  103954  "103954 21:00"**
 2014-04-16 20:00:00    39421   null
 2014-04-16 19:00:00    17269   null
 2014-04-16 18:00:00    14950   null
 2014-04-16 17:00:00    13813   null
 2014-04-16 16:00:00    13246   null
 2014-04-16 15:00:00    12882   null
 2014-04-16 14:00:00    13512   null
 2014-04-16 13:00:00    13228   null
 2014-04-16 12:00:00    12850   null
 2014-04-16 11:00:00    12918   null
 2014-04-16 10:00:00    11923   null
 2014-04-16 09:00:00    11480   null
 2014-04-16 08:00:00    10890   null
 2014-04-16 07:00:00    10285   null
 2014-04-16 06:00:00    10350   null
 2014-04-16 05:00:00    10151   null
 2014-04-16 04:00:00    10917   null
 2014-04-16 03:00:00    12714   null
 2014-04-16 02:00:00    14758   null
 2014-04-16 01:00:00    18018   null
 2014-04-16 00:00:00    21584   null

提前致谢

1 个答案:

答案 0 :(得分:0)

我怀疑您发布的查询中存在其他一些问题,但这可能是因为您尝试简化问题。忽略其他任何事情,我相信你可以通过添加带有SUBQUERY的CASE来实现你的目标。我的下面是一个例子,你可以输出你想要的任何东西。

select date_format(timestamp, '%Y-%m-%d %H:00:00') as hour
     , current_value
     , CASE WHEN current_value = (SELECT MAX(current_value) 
                                    FROM History_Data 
                                   WHERE (timestamp >= (NOW() - interval 24 hour)) 
                                     AND metric_id = '4') 
            THEN 'I AM MAX' ELSE NULL 
        END null_value
  from History_Data 
 where (timestamp >= (now() - interval 24 hour)) 
    and metric_id = '4'
  group by hour  
  order by hour desc
  limit 24

我不相信current_value的使用,因为它既不是不在group by子句中的聚合。在SQL Server中会出错,但在MySQL中它会返回一个值。我怀疑默认它是一个ANY(current_value),这可能就是你没有看到你想要的结果的原因。

您可以通过替换:

来解决此问题
     , MAX(current_value)
     , CASE WHEN MAX(current_value) = (SELECT MAX(current_value) 
                                    FROM History_Data 
                                   WHERE (timestamp >= (NOW() - interval 24 hour)) 
                                     AND metric_id = '4') 
            THEN 'I AM MAX' ELSE NULL 
        END null_value

     , current_value
     , CASE WHEN current_value = (SELECT MAX(current_value) 
                                    FROM History_Data 
                                   WHERE (timestamp >= (NOW() - interval 24 hour)) 
                                     AND metric_id = '4') 
            THEN 'I AM MAX' ELSE NULL 
        END null_value

我制作了一个fiddle,显示了一个类似的结构,并大大简化了用例。它展示了" any"问题和我提出的解决方案。

CREATE TABLE tbl (id INT, val INT);
INSERT INTO tbl VALUES (1,0),(1,1),(2,0),(3,1); 

SELECT id
      ,val --
      ,CASE WHEN val = (SELECT MAX(val) FROM tbl) 
            THEN 'I AM MAX' ELSE NULL 
        END ValIsMax
      ,CASE WHEN MAX(val) = (SELECT MAX(val) FROM tbl) 
            THEN 'I AM MAX' ELSE NULL 
        END GroupMaxValIsMax
      ,MAX(val)
  FROM tbl
 GROUP BY id