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)
 Select help please

Author  Topic 

SteveInBeloit
Starting Member

1 Post

Posted - 2007-10-12 : 16:31:44
Hi,
Two tables, Account and Details:
Account has Acctnum and Desc
Detail has Acctnum, Date, and Balance.

For each Accntnum in Account, I have multiple rows in Details with that acctnum, a date, and a balance.

I need to select all accounts form Account, and the balance from the Details row that has the most recent Date on it. So the most recent balance.

??

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2007-10-12 : 16:41:16
Use a JOIN between the two tables, GROUP BY the accountnum, and MAX on the date.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/
Go to Top of Page

jackv
Master Smack Fu Yak Hacker

2179 Posts

Posted - 2007-10-15 : 04:28:22
SELECT MAX(date), AccountNum FROM Account as A
INNER JOIN Detail as D ON A.AccountNum = D.AccountNum
GROUP BY accountNum

Jack Vamvas
--------------------
Search IT jobs from multiple sources- http://www.ITjobfeed.com
Go to Top of Page
   

- Advertisement -