I have a database that contains serval tables that hold information about the packages we have deployed into our various environments. This database is then interrogated to give an overall picture of the latest versions of the software that are installed which is displayed in a table on a web page.
What I am trying to do, and am struggling with is get the latest version of each package that is installed in each environment. I started to use the information in this post:
Retrieving the last record in each group
Which helped a lot with another aspect of the software, but where I am falling down is because I have more than one criteria on which to select. The relevant parts of the database are as follows
Package Versions table
package_versions
id
server_name_id
package_name_id
version
Package Name table
package_names
id
name
Server Name table
server_names
id
name
environment_name_id
Finally the Environment Names table
environment_names
id
name
This structure allows me to hold a history of the packages and the versions that are deployed into the various environments.
Now what I need to do it get the latest versions of the all the packages that are deployed in each environment. As I said I have been successful with the solution in the post above when I am playing with one table and just two criteria, but here it is 4 tables and 2 criteria.
My attempts either give me back every version that has been installed in the environment:
SELECT environment_names.name as environment,
package_names.display as package,
package_names.name as pkg_name,
package_versions.version as package_version
FROM package_versions INNER JOIN package_names ON package_names.id = package_versions.package_name_id
INNER JOIN server_names ON server_names.id = package_versions.server_name_id
INNER JOIN environment_names ON environment_names.id = server_names.environment_name_id
LEFT JOIN package_versions pv ON (package_versions.package_name_id = pv.package_name_id
AND package_versions.id < pv.id)
AND pv.id IS NULL
ORDER BY environment_names.name, package_names.name
It is not possible to just use Max on the version number as it is generated by Maven so it is in the format x.x.x and can have SNAPSHOT in it.
Other attempts just provide me with the latest version installed anywhere.
I think I need to add something in the LEFT join part but I am really struggling to work out what. Any help is gratefully recieved.
Thanks very much,
Russell