Mysql / Select / MAX()/ GROUP BY

时间:2014-04-10 10:35:35

标签: mysql select join group-by max

这里是数据库的结构(简化):

CREATE TABLE IF NOT EXISTS `TK_servers` (
  `id` int(11),
  `server_id` varchar(11) ,
  `worker_id` varchar(20) ,
  `date_ins` datetime ,
  `avg` varchar(10)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1  ;

INSERT INTO `TK_servers` (`id`, `server_id`, `worker_id`, `date_ins`, `avg`)
VALUES
(108253, 12, 'srva', '2014-04-10 12:00:31', ''),
(108250, 31, 'srva', '2014-04-10 12:00:31', '2.337'),
(108249, 19, 'srva', '2014-04-10 12:00:31', '5.883'),
(108248, 10, 'srva', '2014-04-10 12:00:31', '8.813'),
(108223, 12, 'srvb', '2014-04-10 12:00:22', ''),
(108220, 31, 'srvb', '2014-04-10 12:00:22', '31.183'),
(108219, 19, 'srvb', '2014-04-10 12:00:22', '34.412'),
(108218, 10, 'srvb', '2014-04-10 12:00:22', '36.724'),
(108193, 12, 'srva', '2014-04-10 11:55:31', ''),
(108190, 31, 'srva', '2014-04-10 11:55:31', '2.500'),
(108189, 19, 'srva', '2014-04-10 11:55:31', '5.835'),
(108188, 10, 'srva', '2014-04-10 11:55:31', '8.883'),
(108163, 12, 'srvb', '2014-04-10 11:55:23', ''),
(108160, 31, 'srvb', '2014-04-10 11:55:23', '30.766'),
(108159, 19, 'srvb', '2014-04-10 11:55:23', '42.801'),
(108158, 10, 'srvb', '2014-04-10 11:55:23', '37.093'),
(108133, 12, 'srva', '2014-04-10 11:50:31', ''),
(108130, 31, 'srva', '2014-04-10 11:50:31', '2.538'),
(108129, 19, 'srva', '2014-04-10 11:50:31', '5.397'),
(108128, 10, 'srva', '2014-04-10 11:50:31', '8.737'),
(108103, 12, 'srvb', '2014-04-10 11:50:22', ''),
(108099, 31, 'srvb', '2014-04-10 11:50:22', '30.948'),
(108098, 19, 'srvb', '2014-04-10 11:50:22', '34.279'),
(108097, 10, 'srvb', '2014-04-10 11:50:22', '36.418'),
(108073, 12, 'srva', '2014-04-10 11:45:32', ''),
(108070, 31, 'srva', '2014-04-10 11:45:32', '2.502'),
(108069, 19, 'srva', '2014-04-10 11:45:32', '5.568'),
(108068, 10, 'srva', '2014-04-10 11:45:32', '9.202'),
(108043, 12, 'srvb', '2014-04-10 11:45:22', ''),
(108041, 19, 'srvb', '2014-04-10 11:45:22', '46.626'),
(108039, 31, 'srvb', '2014-04-10 11:45:22', '30.827'),
(108038, 10, 'srvb', '2014-04-10 11:45:22', '37.197'),
(108013, 12, 'srva', '2014-04-10 11:40:32', ''),
(108010, 31, 'srva', '2014-04-10 11:40:32', '2.604'),
(108009, 19, 'srva', '2014-04-10 11:40:32', '5.252'),
(108008, 10, 'srva', '2014-04-10 11:40:32', '8.816'),
(107983, 12, 'srvb', '2014-04-10 11:40:22', ''),
(107980, 31, 'srvb', '2014-04-10 11:40:22', '31.632'),
(107979, 19, 'srvb', '2014-04-10 11:40:22', '43.913'),
(107978, 10, 'srvb', '2014-04-10 11:40:22', '36.796'),
(107953, 12, 'srva', '2014-04-10 11:35:32', ''),
(107950, 31, 'srva', '2014-04-10 11:35:32', '2.369'),
(107949, 19, 'srva', '2014-04-10 11:35:32', '5.876'),
(107948, 10, 'srva', '2014-04-10 11:35:32', '8.721'),
(107923, 12, 'srvb', '2014-04-10 11:35:23', ''),
(107920, 31, 'srvb', '2014-04-10 11:35:23', '31.595'),
(107919, 19, 'srvb', '2014-04-10 11:35:23', '45.858'),
(107918, 10, 'srvb', '2014-04-10 11:35:23', '36.661'),
(107893, 12, 'srva', '2014-04-10 11:30:31', ''),
(107890, 31, 'srva', '2014-04-10 11:30:31', '2.508'),
(107889, 19, 'srva', '2014-04-10 11:30:31', '6.308'),
(107888, 10, 'srva', '2014-04-10 11:30:31', '38.151'),
(107863, 12, 'srvb', '2014-04-10 11:30:23', ''),
(107860, 31, 'srvb', '2014-04-10 11:30:23', '31.162'),
(107859, 19, 'srvb', '2014-04-10 11:30:23', '34.515'),
(107858, 10, 'srvb', '2014-04-10 11:30:23', '36.720'),
(107833, 12, 'srva', '2014-04-10 11:25:31', ''),
(107830, 31, 'srva', '2014-04-10 11:25:31', '2.722'),
(107829, 19, 'srva', '2014-04-10 11:25:31', '5.965'),
(107828, 10, 'srva', '2014-04-10 11:25:31', '8.642'),
(107803, 12, 'srvb', '2014-04-10 11:25:23', ''),
(107799, 31, 'srvb', '2014-04-10 11:25:23', '31.525'),
(107798, 19, 'srvb', '2014-04-10 11:25:23', '34.465'),
(107797, 10, 'srvb', '2014-04-10 11:25:23', '36.893'),
(107773, 12, 'srva', '2014-04-10 11:20:30', ''),
(107770, 31, 'srva', '2014-04-10 11:20:30', '2.373'),
(107769, 19, 'srva', '2014-04-10 11:20:30', '5.206'),
(107768, 10, 'srva', '2014-04-10 11:20:30', '8.749'),
(107743, 12, 'srvb', '2014-04-10 11:20:23', ''),
(107740, 31, 'srvb', '2014-04-10 11:20:23', '31.053'),
(107739, 19, 'srvb', '2014-04-10 11:20:23', '35.096'),
(107738, 10, 'srvb', '2014-04-10 11:20:23', '36.680'),
(107713, 12, 'srva', '2014-04-10 11:15:31', ''),
(107710, 31, 'srva', '2014-04-10 11:15:31', '2.489'),
(107709, 19, 'srva', '2014-04-10 11:15:31', '6.519'),
(107708, 10, 'srva', '2014-04-10 11:15:31', '15.279'),
(107683, 12, 'srvb', '2014-04-10 11:15:23', ''),
(107680, 31, 'srvb', '2014-04-10 11:15:23', '31.236'),
(107679, 19, 'srvb', '2014-04-10 11:15:23', '34.834'),
(107678, 10, 'srvb', '2014-04-10 11:15:23', '37.294'),
(107653, 12, 'srva', '2014-04-10 11:10:31', ''),
(107650, 31, 'srva', '2014-04-10 11:10:31', '2.561'),
(107649, 19, 'srva', '2014-04-10 11:10:31', '5.446'),
(107648, 10, 'srva', '2014-04-10 11:10:31', '8.763'),
(107623, 12, 'srvb', '2014-04-10 11:10:22', ''),
(107620, 31, 'srvb', '2014-04-10 11:10:22', '30.716'),
(107619, 19, 'srvb', '2014-04-10 11:10:22', '34.679'),
(107618, 10, 'srvb', '2014-04-10 11:10:22', '36.854'),
(107593, 12, 'srva', '2014-04-10 11:05:32', ''),
(107590, 31, 'srva', '2014-04-10 11:05:32', '2.355'),
(107589, 19, 'srva', '2014-04-10 11:05:32', '5.720'),
(107588, 10, 'srva', '2014-04-10 11:05:32', '10.779'),
(107563, 12, 'srvb', '2014-04-10 11:05:22', ''),
(107560, 31, 'srvb', '2014-04-10 11:05:22', '31.217'),
(107559, 19, 'srvb', '2014-04-10 11:05:22', '35.360'),
(107558, 10, 'srvb', '2014-04-10 11:05:22', '37.079'),
(107533, 12, 'srva', '2014-04-10 11:00:32', ''),
(107530, 31, 'srva', '2014-04-10 11:00:32', '2.526'),
(107529, 19, 'srva', '2014-04-10 11:00:32', '5.949'),
(107528, 10, 'srva', '2014-04-10 11:00:32', '8.691');

我想列出与worker_id相对应的MAX(平均值)。

SELECT * FROM  `TK_servers`
JOIN (SELECT max(`avg`), (`id`) id FROM `TK_servers`
 WHERE `server_id` = '31'
 GROUP BY LEFT(date_ins,16) )
t2 ON t2.id = `TK_servers`.`id`

结果:

ID     SERVER_ID  WORKER_ID   DATE_INS                        AVG     MAX(`AVG`)
108250    31      srva        April, 10 2014 12:00:31+0000    2.337   31.183 
108190    31      srva        April, 10 2014 11:55:31+0000    2.500   30.766 
108130    31      srva        April, 10 2014 11:50:31+0000    2.538   30.948 
108070    31      srva        April, 10 2014 11:45:32+0000    2.502   30.827 
......

错误,max(avg)是worker_id srvb

3 个答案:

答案 0 :(得分:0)

比较和对比......

SELECT * FROM tk_servers ORDER BY avg DESC LIMIT 1;
+--------+-----------+-----------+---------------------+-------+
| id     | server_id | worker_id | date_ins            | avg   |
+--------+-----------+-----------+---------------------+-------+
| 108068 | 10        | srva      | 2014-04-10 11:45:32 | 9.202 |
+--------+-----------+-----------+---------------------+-------+

SELECT * FROM tk_servers ORDER BY avg+0 DESC LIMIT 1;
+--------+-----------+-----------+---------------------+--------+
| id     | server_id | worker_id | date_ins            | avg    |
+--------+-----------+-----------+---------------------+--------+
| 108041 | 19        | srvb      | 2014-04-10 11:45:22 | 46.626 |
+--------+-----------+-----------+---------------------+--------+

因此,希望这强调了使用正确数据类型的重要性。在这种情况下,它应该是DECIMAL(6,3)NULL,而不是VARCHAR!

答案 1 :(得分:0)

问题是您的avgerage列位于varchar,您必须更改。

然后使用代码:

select id, server_id, worker_id, date_ins ,max(avg) from tk_servers

http://sqlfiddle.com/#!2/4d3529/32

更新:1

SELECT * 
FROM  `TK_servers`
JOIN 
(
SELECT 
  max(`avg`), 
      (`id`) id 
  FROM `TK_servers`
  WHERE `server_id` = '31'
  GROUP BY LEFT(date_ins,16) 
)t2 
ON t2.id = `TK_servers`.`id`
LIMIT 1;

http://sqlfiddle.com/#!2/4d3529/35

答案 2 :(得分:0)

将AVG字段的数据类型从varchar更改为double,然后尝试获取它将起作用的MAX(AVG)。

希望avg正在解决问题