Hi there, I hope your help.
I have this data table in database MySQL.
+---------------------+---------------------+--------+----+
| Start___Date | End___Date | Coding | ID |
+---------------------+---------------------+--------+----+
| 2014-12-16 18:08:51 | 2014-12-16 20:00:16 | 7605 | 1 |
| 2014-12-16 22:01:39 | 2014-12-16 23:36:36 | 9905 | 2 |
| 2014-12-16 22:45:00 | 2014-12-16 23:36:36 | 9905 | 3 |
| 2014-12-16 23:18:32 | 2014-12-16 23:19:04 | 9905 | 4 |
+---------------------+---------------------+--------+----+
In this table I have three identical codings: 9905.
I need with one query select all rows when the same coding is repeated within an hour of the time of start date.
I need select the old datetime with the same code.
In this case I need exclude in my select query the row number three.
+---------------------+---------------------+--------+----+
| Start___Date | End___Date | Coding | ID |
+---------------------+---------------------+--------+----+
| 2014-12-16 18:08:51 | 2014-12-16 20:00:16 | 7605 | 1 |
| 2014-12-16 22:01:39 | 2014-12-16 23:36:36 | 9905 | 2 |
| 2014-12-16 23:18:32 | 2014-12-16 23:19:04 | 9905 | 4 |
+---------------------+---------------------+--------+----+
Because the same coding 9905 is recorded in the table twice within an hour compared to the start time (22:01) and save the record number 4 with coding 9905 because the start time (23:18) is greater compared to the start time (22:01) with the same coding.
I have tried this query JOIN but the output is wrong:
mysql> SELECT
CB.Start___Date,
CB.End___Date,
CB.Coding
FROM
doTable AS CB
JOIN doTable AS CD ON (CB.Coding = CD.Coding)
WHERE
TIME_TO_SEC(
TIMEDIFF(
CB.Start___Date,
CD.Start___Date
)
) / 60 > 60
GROUP BY
CB.Coding;
+---------------------+---------------------+--------+
| Start___Date | End___Date | Coding |
+---------------------+---------------------+--------+
| 2014-12-16 23:18:32 | 2014-12-16 23:19:04 | 9905 |
+---------------------+---------------------+--------+
1 row in set
Can you help me? Thank you in advance.
The doTable below:
DROP TABLE IF EXISTS `doTable`;
CREATE TABLE `doTable` (
`Coding` int(11) DEFAULT NULL,
`End___Date` datetime DEFAULT NULL,
`Start___Date` datetime DEFAULT NULL,
`ID` int(11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`ID`)
) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=latin1;
INSERT INTO `doTable` VALUES ('7605', '2014-12-16 20:00:16', '2014-12-16 18:08:51', '1');
INSERT INTO `doTable` VALUES ('9905', '2014-12-16 23:36:36', '2014-12-16 22:01:39', '2');
INSERT INTO `doTable` VALUES ('9905', '2014-12-16 23:36:36', '2014-12-16 22:45:00', '3');
INSERT INTO `doTable` VALUES ('9905', '2014-12-16 23:19:04', '2014-12-16 23:18:32', '4');