0

我有下表:

++++++++++++++++++++++++++++++++++++++++++++++++++
| location | server | datetime         | max_cpu |
++++++++++++++++++++++++++++++++++++++++++++++++++
| Chicago  | 1      | 2013-05-01 00:00 | 10      |
| Chicago  | 1      | 2013-05-01 01:00 | 15      |
| Chicago  | 1      | 2013-05-01 02:00 | 11      |
| Chicago  | 2      | 2013-05-01 00:00 | 8       |
| Chicago  | 2      | 2013-05-01 01:00 | 12      |
| Chicago  | 2      | 2013-05-01 02:00 | 13      |
| Atlanta  | 1      | 2013-05-01 00:00 | 11      |
| Atlanta  | 1      | 2013-05-01 01:00 | 12      |
| Atlanta  | 1      | 2013-05-01 02:00 | 19      |
| Atlanta  | 2      | 2013-05-01 00:00 | 21      |
| Atlanta  | 2      | 2013-05-01 01:00 | 15      |
| Atlanta  | 2      | 2013-05-01 02:00 | 17      |

我需要给定日期每个位置的每个盒子的最大 CPU,例如

++++++++++++++++++++++++++++++++++++++++++++++++++
| location | server | datetime         | max_cpu |
++++++++++++++++++++++++++++++++++++++++++++++++++
| Chicago  | 1      | 2013-05-01 01:00 | 15      |
| Chicago  | 2      | 2013-05-01 02:00 | 13      |
| Atlanta  | 1      | 2013-05-01 02:00 | 19      |
| Atlanta  | 2      | 2013-05-01 00:00 | 21      |

我知道如何针对单个标准(例如仅位置)执行此操作,并尝试对此进行扩展(见下文),但它没有给我所需的输出。

SELECT a.location, a.server, a.datetime, a.max_cpu 
  FROM mytable as a INNER JOIN 
  (
    SELECT location, server, max(max_cpu) as max_cpu
    FROM mytable
    GROUP BY location, server
  ) 
  AS b ON 
  (
    a.location = b.location
    AND a.server = b.server
    AND a.max_cpu = b.max_cpu
  )
4

3 回答 3

2

您可以通过找到最大 cpu 并重新连接到原始表来做到这一点。

似乎您想要最大值的时间以及数量(这在文本中没有明确说明,但在结果中很清楚):

select t.*
from mytable t join
     (select location, server, DATE(datetime) as thedate, MAX(max_cpu) as maxmaxcpu
      from mytable t
      group by location, server, DATE(datetime)
     ) lsd
     on lsd.location = t.location and lsd.server = t.server and
        lsd.thedate = DATE(t.datetime) and lsd.maxmaxcpu = t.max_cpu

这会计算每天的 maxcpu,然后返回以获取原始数据中的适当行或行。如果有多个记录的最大值,您将获得所有记录。如果您只想要一个,您可以添加group by location, server, day(datetime)到查询中。

于 2013-05-01T14:21:37.943 回答
1

这更好地回答了问题的“特定日期”部分。由于您可以忽略时间,这避免了日期 hacky 的事情,有点简单,并且如果多次具有该服务器的相同 CPU,它不会显示重复:

select distinct a.location, a.server, a.datetime, a.max_cpu
from 
  mytable a
  inner join (
    select location, server, max(max_cpu) as max
    from mytable
    where
      datetime >= ? -- start of day
      and datetime < ? -- start of next day
    group by location, server
  ) b on a.location=b.location and a.server=b.server and a.max_cpu as max
where
  a.datetime >= ? -- start of day
  a.and datetime < ? -- start of next day
于 2013-05-01T14:21:25.963 回答
0

查询(仅当 max_cpu 每个位置、服务器和 Date 唯一时才有效):

SQLFIDDLE示例

SELECT t1.*
FROM Table1 t1
WHERE t1.max_cpu = (SELECT MAX(t2.max_cpu)
                    FROM Table1 t2
                    WHERE t2.location = t1.location 
                    AND t2.server = t1.server
                    AND DATE(t2.datetime) = DATE(t1.datetime))

结果:

| LOCATION | SERVER |                   DATETIME | MAX_CPU |
------------------------------------------------------------
|  Chicago |      1 | May, 01 2013 01:00:00+0000 |      15 |
|  Chicago |      2 | May, 01 2013 02:00:00+0000 |      13 |
|  Atlanta |      1 | May, 01 2013 02:00:00+0000 |      19 |
|  Atlanta |      2 | May, 01 2013 00:00:00+0000 |      21 |
于 2013-05-01T15:09:13.513 回答