2014-02-06 44 views
0

我跟蹤了從3個表中獲取數據的查詢:index_data,data_binmetrics如何將「group by」和max值用於內部連接查詢?

SELECT 
    index_data.host_name,   
    data_bin.value 
FROM 
    metrics 
INNER JOIN 
    index_data ON index_data.id = metrics.index_id 
INNER JOIN 
    data_bin ON data_bin.id_metric = metrics.metric_id 
WHERE 
    metrics.metric_name = 'avg'; 

下面是結果:

host_name  | value 
================================ 
namenode1   1.125 
namenode1   1 
namenode1   1 
namenode1   1 
namenode1   1 
namenode1   4.875 
namenode1   1 
namenode1   0.875 
Centreon-Server  2 
Centreon-Server  1 
Centreon-Server  2 
Centreon-Server  1 
Centreon-Server  5 
Centreon-Server  1 
Centreon-Server  1 
Centreon-Server  2 

我的目標是讓每host_name只有一條記錄,最大value

所以預期輸出應該是:

namenode1   4.875 
Centreon-Server 5 

我怎樣才能做到這一點?

感謝,

+0

注意,如果沒有ORDER限制由是有點意義的 – Strawberry

+0

@Strawberry對不起,移除,結果300K – snaggs

回答

1

我想下面將幫助您

SELECT 
    index_data.host_name,   
    Max(data_bin.value) as value 
     FROM metrics 
    inner join index_data ON index_data.id = metrics.index_id 
    inner join data_bin ON data_bin.id_metric = metrics.metric_id 

where metrics.metric_name = 'avg' AND 
FROM_UNIXTIME(data_bin.ctime) between date('2014-02-05 16:15:24') and date('2014-02-06 16:15:24') 
Group by index_data.host_name;