| Author |
Topic |
|
forthemame
Starting Member
4 Posts |
Posted - 2009-09-22 : 19:45:26
|
| Probably not the best of subject lines, but hopefully the following description will clear things up a bit.I have two tables: People and Class Info. The People table stores names and SSN's, while the Class Info table stores information about classes they've completed and the year in which they completed them in.PersonSSN Name111 Adam222 Bill333 CarolClass InfoSSN ClassID Year111 1 2005111 2 2006111 3 2006222 1 2008222 3 2008333 1 2007333 4 2008How do I write a query to get the only the class info for the most recent year a person took a class? I tried the following, but it seems to only grab the maximum year overall instead of the maximum year per person.Select P.Name, C.ClassID, C.YearFrom Person P, Class_Info CWhere C.ClassID = (Select MAX(C1.ClassID) From Class_Info C1 Where P.SSN = C1.SSN)The above query returnsBill, 1, 2008Bill, 2, 2008Carol, 4, 2008But I would like it to returnAdam, 2, 2006Adam, 3, 2006Bill, 1, 2008Bill, 2, 2008Carol, 4, 2008Thoughts? Thanks! |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
|
|
forthemame
Starting Member
4 Posts |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
forthemame
Starting Member
4 Posts |
Posted - 2009-09-24 : 14:20:48
|
quote: Originally posted by tkizer Umm okay. I suppose it would have been better if you explained why it didn't help that way we can try again.Tara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/Subscribe to my blog"Let's begin with the premise that everything you've done up until this point is wrong."
Well I'm working in Peoplesoft which uses a different syntax of SQL it seems. Also this is just a small portion of my query, as the main one is much closer to ~100 lines. That makes it hard to plug in this small piece in the large puzzle.Finally I'm not entirely sure I see your query working correctly actually. |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
forthemame
Starting Member
4 Posts |
|
|
|