I have a change-log table that I only want to display the most recent entry for each document. But, when the document number has two trailing zeros, the query does not perform correctly. Any ideas how to fix it?
Example table:
number | changedatetime |
---|---|
1-050 | 2020-12-10 |
1-100 | 2020-12-10 |
1-123 | 2020-12-10 |
1-050 | 2020-12-15 |
1-100 | 2020-12-16 |
1-123 | 2020-12-17 |
Example query:
SELECT mdt.*
FROM change_log mdt
LEFT JOIN change_log bdt ON mdt.number = bdt.number
AND mdt.changedatetime < bdt.changedatetime
WHERE bdt.changedatetime IS NULL
AND mdt.document='$doctype'
ORDER BY mdt.number ASC
I expect to get a list of just the most recent like:
number | changedatetime |
---|---|
1-050 | 2020-12-15 |
1-100 | 2020-12-16 |
1-123 | 2020-12-17 |
But, I'm getting the most recent of all of them unless the document number has two trailing zeros:
number | changedatetime |
---|---|
1-050 | 2020-12-15 |
1-123 | 2020-12-17 |
The SQL database type for document number is "text" and changedatetime is "timestamp."