Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6878947
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:47:10+00:00 2026-05-27T04:47:10+00:00

I would prefer to get this done in MySQL. Table: tracker | user_id |

  • 0

I would prefer to get this done in MySQL.

Table:

tracker | user_id | timestamp | action
1         1         1234        1
2         2         1236        9
3         1         1237        2

I need to find all users who had the action of 1, and subtract the timestamp from their next action. Meaning, User 1’s 1234 timestamp will be subtracted from his 1237 timestamp.

The idea is to calculate how long the user spent at action 1 until they went somewhere else.

Getting the first timestamp is easy. One idea is to record the tracker value, and then do a subquery for where user_id pops up again with a tracker higher than the first. In the example: WHERE user_id = 1 AND tracker > 1

Is there a more optimal solution?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T04:47:10+00:00Added an answer on May 27, 2026 at 4:47 am

    Probably this helps you. This will join to all future actions

    SELECT t1.*, 
           t2.`timestamp` - t1.`timestamp`
    FROM   table t1 
           JOIN table t2 
             ON t1.user_id = t2.user_id 
                AND t2.tracker > t1.tracker 
    WHERE  t1.`action` = 1;
    

    you probably want to extend to add also a condition to get the next one only.

    this is some ad-hoc query, and can’t precisely tell if will work or not
    This is suppose to select only the next one for that user.

    SELECT t1.*, 
           t2.`timestamp` - t1.`timestamp` 
    FROM   table t1 
           JOIN table t2 
             ON t1.user_id = t2.user_id 
                AND t2.tracker = (SELECT Min(tracker) 
                                  FROM   table t3 
                                  WHERE  t3.user_id = t1.user_id 
                                         AND t3.tracker > t1.tracker) 
    WHERE  t1.`action` = 1; 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to access a mysql database from c# code but I would prefer
I need to do some process injection using C++ but I would prefer to
I need to create hot-keys for every control + number combination and would prefer
I would prefer that a console app would default to multithreaded debug. warning level
I have some template code that I would prefer to have stored in a
I work in a Windows environment and would prefer to deploy code to IIS.
Poppler is a classic example of something without documentation that you would prefer be
My customer doesn't want a database but would prefer to update their data in
I have some user controls that I'm loading in SharePoint and I would prefer
What is a good way to perform animation using .NET? I would prefer not

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.