I have a table like the following in MySQL 5.1:
+--------------+----------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+----------------+------+-----+---------+----------------+
| log_id | int(11) | NO | PRI | NULL | auto_increment |
| date | datetime | NO | MUL | NULL | |
| date_millis | int(3) | NO | | NULL | |
| eib_address | varchar(20) | NO | | NULL | |
| ip_address | varchar(15) | NO | | NULL | |
| value | decimal(20,10) | NO | MUL | NULL | |
| application | tinyint(4) | NO | | NULL | |
| phys_address | varchar(20) | NO | | NULL | |
| orig_log_id | bigint(20) | NO | | NULL | |
+--------------+----------------+------+-----+---------+----------------+
In this table, log_id
and orig_log_id
are always unique. It is possible that two rows may have duplicate values for any of the other fields, though. Ignoring the *log_id
fields, our problem is that two rows may be identical in all other columns, but have differing values for value
. I am trying to figure out the correct SQL query to identify when two (or more) rows have identical values for date
, date_millis
and eib_address
, but different values for value
, log_id
and orig_log_id
. So far, I've been able to come up with a query that accomplishes the first clause in my previous sentence:
SELECT main.*
FROM sensors_log main
INNER JOIN
(SELECT date, date_millis, eib_address
FROM sensors_log
GROUP BY date, date_millis, eib_address
HAVING count(eib_address) > 1) dupes
ON main.date = dupes.date
AND main.date_millis = dupes.date_millis
AND main.eib_address = dupes.eib_address;
However, I can't seem to figure out when value
differs. I at least know that just throwing AND main.value != dupes.value
into the ON
clause doesn't do it!