Okay, I´m working on a website right now that shows information about parts of electronic devices. These parts sometimes get a revision. The part number stays the same, but they append an A, B, C etc to the part number, so the ´higher´ the letter, the newer it is. Also a date is added. So the table looks something like this:
------------------------------------------------------------
| Partcode | Description | Partdate |
------------------------------------------------------------
| 12345A | Some description 1 | 2009-11-10 |
| 12345B | Some description 2 | 2010-12-30 |
| 17896A | Some description 3 | 2009-01-12 |
| 12345C | Some description 4 | 2011-08-06 |
| 17896B | Some description 5 | 2009-07-10 |
| 12345D | Some description 6 | 2012-05-04 |
------------------------------------------------------------
What I need right now is the data from the newest revision of a part. So for this example I need:
12345D and 17896B
The query that some has build before me is something in the line of this:
SELECT substring(Partcode, 1, 5) AS Part,
(
SELECT pt.Partcode
FROM Parttable pt
WHERE substring(pt.PartCode, 1, 5) = Part
ORDER BY pt.Partdate DESC
LIMIT 0,1
),
(
SELECT pt.Description
FROM Parttable pt
WHERE substring(pt.PartCode, 1, 5) = Part
ORDER BY pt.Partdate DESC
LIMIT 0,1
),
(
SELECT pt.Partdate
FROM Parttable pt
WHERE substring(pt.PartCode, 1, 5) = Part
ORDER BY pt.Partdate DESC
LIMIT 0,1
)
FROM Parttable
GROUP BY Part
As you will understand, this query is insanely slow and feels really inefficient. But I just can't get my head around how to optimize this query.
So I really hope someone can help.
Thanks in advance!
PS. I'm working on a MySQL database and before anyone asks, I can't change the database.