我的数据库方案,
CREATE TABLE `result` (
`ID` bigint(21) NOT NULL AUTO_INCREMENT,
`datetime` datetime DEFAULT NULL,
`recordnum` int(11) DEFAULT NULL,
`recordtype` int(11) DEFAULT NULL,
`minvalue` int(11) DEFAULT NULL,
`maxvalue` int(11) DEFAULT NULL,
`data1` int(11) DEFAULT NULL,
`data2` int(11) DEFAULT NULL,
`area` varchar(32) DEFAULT NULL,
PRIMARY KEY (`ID`),
UNIQUE KEY `res` (`datetime`,`recordnum`,`area`)
) ENGINE=MyISAM AUTO_INCREMENT=7 DEFAULT CHARSET=latin1;
数据样本
INSERT INTO `result` VALUES ('1', '2013-03-26 12:03:31', '2', '1', '5', '10', '100', '200','Zone1'); INSERT INTO `result` VALUES ('2', '2013-03-26 12:03:31', '2', '1', '2', '7', '20', '20','Zone2'); INSERT INTO `result` VALUES ('3', '2013-03-26 12:03:31', '5', '2', '1', '2', '8', '15','Zone2'); INSERT INTO `result` VALUES ('4', '2013-03-26 12:00:31', '2', '1', '2', '7', '100', '200','Zone1'); INSERT INTO `result` VALUES ('5', '2013-03-26 12:00:31', '2', '1', '3', '9', '100', '200','Zone2'); INSERT INTO `result` VALUES ('6', '2013-03-26 12:00:31', '5', '2', '7', '2', '4', '10', 'Zone2'); INSERT INTO `result` VALUES ('7', '2013-03-25 12:03:31', '2', '1', '5', '10', '100', '200','Zone1'); INSERT INTO `result` VALUES ('8', '2013-03-25 12:03:31', '2', '1', '2', '7', '20', '20','Zone2'); INSERT INTO `result` VALUES ('9', '2013-03-25 12:03:31', '5', '2', '1', '2', '8', '15','Zone2'); INSERT INTO `result` VALUES ('10', '2013-03-25 12:00:31', '2', '1', '2', '7', '100', '200','Zone1'); INSERT INTO `result` VALUES ('11', '2013-03-25 12:00:31', '2', '1', '3', '9', '100', '200','Zone2'); INSERT INTO `result` VALUES ('12', '2013-03-25 12:00:31', '5', '2', '7', '2', '4', '10','Zone2'); INSERT INTO `result` VALUES ('13', '2013-03-25 11:03:31', '2', '1', '3', '10', '100','200', 'Zone1'); INSERT INTO `result` VALUES ('14', '2013-03-25 11:03:31', '2', '1', '8', '7', '20', '20','Zone2'); INSERT INTO `result` VALUES ('15', '2013-03-25 11:03:31', '5', '2', '2', '2', '8', '15','Zone2'); INSERT INTO `result` VALUES ('16', '2013-03-25 11:00:31', '2', '1', '1', '7', '100', '200','Zone1'); INSERT INTO `result` VALUES ('17', '2013-03-25 11:00:31', '2', '1', '9', '9', '100', '200','Zone2'); INSERT INTO `result` VALUES ('18', '2013-03-25 11:00:31', '5', '2', '12', '2', '4', '10','Zone2');
我想要的是当 minvalue 对于每个recordnum和area以及date都是最小值时查询每个值
+----+---------------------+-----------+-------+----------+----------+-------+-------+------------+
| ID | datetime | recordnum | area |minvalue | maxvalue | data1 | data2 | recordtype |
+----+---------------------+-----------+-------+----------+----------+-------+-------+------------+
| 2 | 2013-03-26 12:03:31 | 2 | Zone2 | 2 | 7 | 20 | 20 | 1 |
| 3 | 2013-03-26 12:03:31 | 5 | Zone2 | 1 | 2 | 8 | 15 | 2 |
| 4 | 2013-03-26 12:00:31 | 2 | Zone1 | 2 | 7 | 100 | 200 | 1 |
| 8 | 2013-03-25 12:03:31 | 2 | Zone2 | 2 | 7 | 20 | 20 | 1 |
| 9 | 2013-03-25 12:03:31 | 5 | Zone2 | 1 | 2 | 8 | 15 | 2 |
| 16 | 2013-03-25 11:00:31 | 2 | Zone1 | 1 | 7 | 100 | 200 | 1 |
+----+---------------------+-----------+-------+----------+----------+-------+-------+------------+
下面这个查询给出了很好的结果,但仍然显示重复记录,请帮助
SELECT a.*
FROM result a
JOIN
(
SELECT datetime as mindatetime,DATE(datetime) dateOnly,area, recordNum, MIN(minvalue) min_val
FROM result
GROUP BY dateOnly,area, recordNum
) b ON a.area = b.area AND
a.recordNUM = b.recordNum AND
a.minvalue = b.min_val ;
+----+---------------------+-----------+------------+----------+----------+-------+-------+-------+
| ID | datetime | recordnum | recordtype | minvalue | maxvalue | data1 | data2 | area |
+----+---------------------+-----------+------------+----------+----------+-------+-------+-------+
| 2 | 2013-03-26 12:03:31 | 2 | 1 | 2 | 7 | 20 | 20 | Zone2 |
| 2 | 2013-03-26 12:03:31 | 2 | 1 | 2 | 7 | 20 | 20 | Zone2 |
| 3 | 2013-03-26 12:03:31 | 5 | 2 | 1 | 2 | 8 | 15 | Zone2 |
| 3 | 2013-03-26 12:03:31 | 5 | 2 | 1 | 2 | 8 | 15 | Zone2 |
| 4 | 2013-03-26 12:00:31 | 2 | 1 | 2 | 7 | 100 | 200 | Zone1 |
| 8 | 2013-03-25 12:03:31 | 2 | 1 | 2 | 7 | 20 | 20 | Zone2 |
| 8 | 2013-03-25 12:03:31 | 2 | 1 | 2 | 7 | 20 | 20 | Zone2 |
| 9 | 2013-03-25 12:03:31 | 5 | 2 | 1 | 2 | 8 | 15 | Zone2 |
| 9 | 2013-03-25 12:03:31 | 5 | 2 | 1 | 2 | 8 | 15 | Zone2 |
| 10 | 2013-03-25 12:00:31 | 2 | 1 | 2 | 7 | 100 | 200 | Zone1 |
| 16 | 2013-03-25 11:00:31 | 2 | 1 | 1 | 7 | 100 | 200 | Zone1 |
+----+---------------------+-----------+------------+----------+----------+-------+-------+-------+