Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Finding a maximum value per account

Author  Topic 

demsoft
Starting Member

1 Post

Posted - 2014-08-26 : 06:10:08
Hi everyone
I'm stuck trying to get some useful information out of a line-of-business system we're using. Essentially there's a table I want to use in order to query onother one, but before I can get that far I need to get the 'latest revision' version for an account.

The table is pretty big & seems poorly designed, but the 3 fields I'm currently interested in are Account, Revision & Display. Account is an account number, Revision and Display are integers (stored as text). NONE of thenm are unique, although the combination of Account & Revision is.
What I want to do is find the highest revision number for each account, & then use the associated Display to link to my other table(s). The SQL I've got so far is :

select account, revision, display
from histhead o
where revision = (
select max(revision)
from histhead
where revision = o.revision)

Unfortunately, because there are multiple accounts of the same value it brings back something like :

account revision display
005598 001 201005 7843
005598 001 310358 17618
005598 002 201005 7844
005598 002 999999 17619
005598 003 201005 7845
005598 003 999999 17620
007475 200810 962
007475 200900 1252
007475 999999 16910
007641 201003 7039
007641 201311 25958

Whereas what I really want is just :

account revision display
005598 001 310358 17618
005598 002 999999 17619
005598 003 999999 17620
007475 999999 16910
007641 201311 25958

Can anyone tell me how to do that, please?

thanks

Mark

gbritton
Master Smack Fu Yak Hacker

2780 Posts

Posted - 2014-08-26 : 08:09:38
I'm confused by the example data:


account revision display
005598 001 201005 7843


has three column headings but four numbers. What goes with what? Is there a missing column heading?
Go to Top of Page
   

- Advertisement -