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.

    Requires Free Membership to View

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

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

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: