Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


SQL query to get the last selected value per person


SQL query to get the last selected value per person

Author
Message
t.busser
t.busser
Forum Newbie
Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)

Group: General Forum Members
Points: 7 Visits: 7
Hi,

I need some help with a SQL query. I've been trying to find a way to get the desired information but so far nothing seems to work right. I have a table which looks like this:
personId int, selectedValue int, logDate datetime


For each person there can be multiple records in the table. What I'd like to do is get the last selected value for each person in the table. I am able to get the last date a person selected something with this query:
SELECT personId, MAX(logDate) FROM MySelectedValues GROUP BY personId 


Unfortunately I've been unable to come up with a query that will also retrieve the selected value. Any help would be much appreciated!
spaghettidba
spaghettidba
SSCertifiable
SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)

Group: General Forum Members
Points: 5730 Visits: 13305
Try with this:


SELECT A.personId, A.LogDate, A.selectedValue
FROM MySelectedValues AS A
INNER JOIN (
SELECT personId, MAX(logDate) AS LogDate
FROM MySelectedValues
GROUP BY personId
) AS B
ON A.personId = B.personId
AND A.LogDate = B.LogDate



Regards
Gianluca

--
Gianluca Sartori

How to post T-SQL questions
spaghettidba.com
@spaghettidba
t.busser
t.busser
Forum Newbie
Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)

Group: General Forum Members
Points: 7 Visits: 7
Thanks, that works really well. You've been a great help!
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search