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

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 に答える