SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Need help with query
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

sync00
Starting Member

24 Posts

Posted - 12/03/2012 :  11:11:33  Show Profile  Reply with Quote
Here is some sample data.

PersonId  Year  Amount
23        2012   5
23        2011  10
25        2011  20
25        2011   6

I need to select the most recent Year and associate amount for each PersonId where the Amount is not 0.

Edited by - sync00 on 12/03/2012 11:35:38

sunitabeck
Flowing Fount of Yak Knowledge

5155 Posts

Posted - 12/03/2012 :  11:21:03  Show Profile  Reply with Quote
SELECT 
	PersonId,
	MAX([Year]) AS MaxYear
FROM
	tbl
WHERE
	Amount <> 0
GROUP BY
	PersonId
Go to Top of Page

sync00
Starting Member

24 Posts

Posted - 12/03/2012 :  11:34:40  Show Profile  Reply with Quote
Thanks. I forgot to mention that I need to return the Amount associated with the most recent year. That is the part I'm stuck on
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52325 Posts

Posted - 12/03/2012 :  11:40:33  Show Profile  Reply with Quote

SELECT t.*
FROM Table t
INNER JOIN (SELECT 
	PersonId,
	MAX([Year]) AS MaxYear
FROM
	tbl
WHERE
	Amount <> 0
GROUP BY
	PersonId
)t1
ON t1.PersonId = t.PersonId
AND t1.MaxYear = t.Year


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

sync00
Starting Member

24 Posts

Posted - 12/03/2012 :  11:56:42  Show Profile  Reply with Quote
Very nice.

Thank you.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52325 Posts

Posted - 12/03/2012 :  12:15:19  Show Profile  Reply with Quote
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000