February 25, 2013 at 4:31 pm
I'm trying to create a query with multiple columns. The CandidateID column includes values that appear more than once. I need my query to return the latest CandidateID value and to exclude the older CandidateID values of the same ID number.
Sample values:
2008432012-02-14 07:49:00
2008432012-02-15 06:56:00
2310222012-02-14 07:49:00
2335252012-01-30 21:15:00
2766992012-01-30 21:14:00
2910192012-01-30 21:15:00
2988052012-01-30 21:15:00
2988052012-02-15 06:56:00
2988052012-02-21 09:39:00
3060112012-02-14 07:49:00
3060112012-02-15 06:56:00
3060112012-02-21 09:55:00
3135382012-01-30 21:15:00
3161352012-01-30 21:14:00
I need the query to return the following...
2008432012-02-15 06:56:00
2310222012-02-14 07:49:00
2335252012-01-30 21:15:00
2766992012-01-30 21:14:00
2910192012-01-30 21:15:00
2988052012-02-21 09:39:00
3060112012-02-21 09:55:00
3135382012-01-30 21:15:00
3161352012-01-30 21:14:00
"Nicholas"
February 25, 2013 at 6:00 pm
5280_Lifestyle (2/25/2013)
I'm trying to create a query with multiple columns. The CandidateID column includes values that appear more than once. I need my query to return the latest CandidateID value and to exclude the older CandidateID values of the same ID number.
Like this?
select CandidateID, max(YourDateColumn)
from YourTable
group by CandidateID
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
February 26, 2013 at 12:50 am
You can also use ROW_NUMBER()
SELECT*
FROM(
SELECTROW_NUMBER() OVER ( PARTITION BY CandidateID ORDER BY YourDateColumn DESC ) AS RN, *
FROMYourTable
) AS YT
WHEREYT.RN = 1
How to post data/code on a forum to get the best help - Jeff Moden
http://www.sqlservercentral.com/articles/Best+Practices/61537/
February 26, 2013 at 3:39 am
but If performance is in concern then go for
select CandidateID, max(YourDateColumn)
from YourTable
group by CandidateID
February 26, 2013 at 3:48 am
BriPan (2/26/2013)
but If performance is in concern then go for
select CandidateID, max(YourDateColumn)
from YourTable
group by CandidateID
Sean's solution is almost certainly quicker than Kingston's ROW_NUMBER method; however, Kingston's solution is a viable alternative which introduces the method to those who may not already be familiar with it - and of course, it demonstrates how to identify the target rows without necessarily filtering out those which are not targeted.
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
February 26, 2013 at 8:05 am
Sean's solution worked. Thank you very much!
"Nicholas"
February 27, 2013 at 7:56 am
5280_Lifestyle (2/26/2013)
Sean's solution worked. Thank you very much!
You are quite welcome. Glad that worked for you.
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply