Q

Latest entry based on timestamp

I have a table with userid, attribute_name, value_date_time. I want to report a single row for each user that shows the latest entry in the table based on the timestamp inserted into value_date_time.

I have a table with userid, attribute_name, value_date_time. I want to report a single row for each user that shows the latest entry in the table based on the timestamp inserted into value_date_time. Any help much appreciated.
You'll have to query for the maximum date attribute and then use that to select the record that matches it. Something similar to the following:
SELECT t1.userid,t1.attribute_name,t1.value_date_time
FROM table_name t1, (SELECT t2.userid,t2.attribute_name,
                     MAX(t2.value_date_time) as max_time
                     FROM table_name) t2
WHERE t1.userid=t2.userid AND t1.attribute_name=t2.attribute_name
  AND t1.value_date_time = t2.max_time;
This was first published in April 2006
This Content Component encountered an error

Pro+

Features

Enjoy the benefits of Pro+ membership, learn more and join.

Have a question for an expert?

Please add a title for your question

Get answers from a TechTarget expert on whatever's puzzling you.

You will be able to add details on the next page.

0 comments

Oldest 

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

-ADS BY GOOGLE

SearchDataManagement

SearchBusinessAnalytics

SearchSAP

SearchSQLServer

TheServerSide

SearchDataCenter

SearchContentManagement

SearchFinancialApplications

Close