Solved

Need to return the most recent sale for each salesman

Posted on 2008-10-21
4
283 Views
Last Modified: 2012-05-05
I have the following data:

TableAutoID    SalespersonID  Sale Date
83                     388                       2008-10-03 12:43:25.320
84                     481                       2008-10-03 12:50:04.807
85                     481                       2008-10-03 12:51:01.150
86                   100                    2008-10-03 12:50:04.807
87                   100                    2008-10-03 12:43:25.320
... and so on, but that's enough to indicate what I need.


I need to return the TableAutoId of the most recent sale for each salespersonID, so based on the records shown above, I need to return 83, 85 and 86. I've gone round and round trying to find some way to do this in a query WITHOUT A CURSOR, and I think it can be done with some sort of correlated subquery selecting from an aliased version of the original table, but thus far, I haven't come up with the solution to what seems like a fairly common query. Thus, I have come to grovel at the feet of the experts at experts exchange. Grovel, grovel.
0
Comment
Question by:nedrich12
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 5

Accepted Solution

by:
jfmador earned 500 total points
ID: 22773019
Try this

SELECT TableAutoID FROM table
INNER JOIN
(SELECT SalesPersonID, MAX(SaleDate) as MAxSaleDate FROM table GROUP BY SalesPersonID) t
ON table.SalesPersonID = t.SalesPersonID and table.SaleDate = t.SaleDate
0
 
LVL 17

Expert Comment

by:HuyBD
ID: 22773022
try this
select * from utable
where [Sale Date]=(select max([Sale Date]) 
from utable as t where t.SalespersonID=utable.SalespersonID)

Open in new window

0
 
LVL 5

Expert Comment

by:jfmador
ID: 22773024
Sorry i made a mistake

SELECT TableAutoID FROM table
INNER JOIN
(SELECT SalesPersonID, MAX(SaleDate) as MAxSaleDate FROM table GROUP BY SalesPersonID) t
ON table.SalesPersonID = t.SalesPersonID and table.SaleDate = t.MaxSaleDate

I mispelled a field from t
0
 

Author Closing Comment

by:nedrich12
ID: 31508589
Thanks for your help. This is exactly what I needed.
0

Featured Post

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Parse this column 6 35
Reformat SQL - so SSRS can read the columns 25 35
How can I find this data? 3 28
Trying to understand why my Index is so large 12 20
Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question