Possible Duplicate:
SQL: Find the max record per group
I have a table with four columns as such:
name major minor revision
p1 0 4 3
p1 1 0 0
p1 1 1 4
p2 1 1 1
p2 2 5 0
p3 3 4 4
This is basically ca table containing records for each version of a program. I want to do a select to get all of the programs and their latest version so the results would look like this:
name major minor revision
p1 1 1 4
p2 2 5 0
p3 3 4 4
I can’t just group by the name and get the max of each column because then i would just end up with the highest number from each column, but not the specific row with the highest version. How can I set this up?
The way I try to solve SQL problems is to take things step by step.
The maximum major number for each product is given by:
The maximum minor number corresponding to the maximum major number for each product is therefore given by:
And the maximum revision (for the maximum minor version number corresponding to the maximum major number for each product), therefore, is given by:
Tested – it works and produces the same answer as Andomar‘s query does.
Performance
I created a bigger volume of data (11616 rows of data), and ran a benchmark timing of Andomar’s query against mine – target DBMS was IBM Informix Dynamic Server (IDS) version 11.70.FC2 running on MacOS X 10.7.2. I used the first of Andomar’s two queries since IDS does not support the comparison notation in the second one. I loaded the data, updated statistics, and ran the queries both with mine followed by Andomar’s and with Andomar’s followed by mine. I also recorded the basic costs reported by the IDS optimizer. The result data from both queries were the same (so the queries are both accurate – or equally inaccurate).
Table unindexed:
Table with unique index on (name, major, minor, revision):
As you can seen, the index dramatically improves the performance of Andomar’s query, but it still seems to be more expensive on this system than my query. The index gives a 25% time saving for my query. I’d be curious to see comparable figures for the two versions of Andomar’s query on comparable volumes of data, with and without the index. (My test data can be supplied if you need it; there were 132 products – the 3 listed in the question and 129 new ones; each new product had (the same) 90 version entries.)
The reason for the discrepancy is that the sub-query in Andomar’s query is a correlated sub-query, which is a relatively expensive process (dramatically so when the index is missing).