Click here to Skip to main content
15,919,341 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a table, MyTable, in which items are stored with their individual reference code, ReferenceCode but there are different versions of each ReferenceCode as the items have been updated over time so we can look back to historical records for each ReferenceCode.

I've written some SQL which pulls out the entries with the highest version number to as to extract the newest version of each ReferenceCode and give me a ‘live’ table. However, it seems to take forever (5.5 MINUTES!) to extract the data into a table - and that's in Access itself!

There are about 16,000 entries in the table, and just over half of those are the current live versions. The table has about 20 fields and yes, I do need to SELECT *.

Is there a quicker way of doing this in a single SQL statement?

What I have tried:

select * from [mytable] where ((([mytable].version)=(select max(x.version) from [mytable] as x where x.referencecode = [mytable].referencecode))) order by referencecode;
Posted
Updated 8-May-17 0:47am
Comments
CHill60 8-May-17 5:09am    
Does this work any better?
select A.* from [myTable] A
INNER JOIN (select referencecode, max([version]) as [version] from [myTable] group by referencecode) B
	ON B.referencecode = A.referencecode AND A.[version]=B.[version]
order by A.referencecode
ICTIS-THL 8-May-17 5:27am    
If you'd care to submit the edited code as solution, I'll gladly accept it. :-)
ICTIS-THL 8-May-17 5:12am    
Nope... "Circular refence caused by alias 'version' in query definition's SELECT list"
ICTIS-THL 8-May-17 5:19am    
But I editied it to:
select A.* from [myTable] A
INNER JOIN (select referencecode, max(myTable.[version]) as [version] from [myTable] group by referencecode) B
ON B.referencecode = A.referencecode AND A.[version]=B.[version]
order by A.referencecode

And that seems blisteringly fast...
CHill60 8-May-17 6:38am    
Yeah - sorry about missing that table alias :facepalm: I tested it in SQL rather than Access and the former is less fussy about stuff like that.
Joins are always faster than straight forward in line sub-queries.
Glad to have helped.

1 solution

OP has confirmed that the following query is much faster (after they corrected my mistake :0)
SQL
select A.* from [myTable] A
INNER JOIN (select referencecode, max(myTable.[version]) as [version] from [myTable] group by referencecode) B
	ON B.referencecode = A.referencecode AND A.[version]=B.[version]
order by A.referencecode

I said in my comment that "Joins are always faster than ... sub-queries" - that is not strictly true. It's true in this case because the original query was using a correlated sub-query - i.e. it was referencing the outer query ...
select * from [mytable] where ((([mytable].version)=(select max(x.version) from [mytable] as x where x.referencecode = [mytable].referencecode))) order by referencecode;

See Correlated subquery - Wikipedia[^]
 
Share this answer
 
Comments
Maciej Los 8-May-17 11:46am    
5ed!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900