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

  • Home
  • SEARCH
  • 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 8955883
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T14:39:20+00:00 2026-06-15T14:39:20+00:00

I have a table like this: ——————————————— |Id | Step | StartedAt | ———————————————

  • 0

I have a table like this:

---------------------------------------------
|Id | Step                  | StartedAt     |
---------------------------------------------
| 1 | Download Data         | 10:20:00      |
| 2 | Data Quality Control  | 10:45:00      |
| 3 | Run Prediction        | 10:47:00      |
---------------------------------------------

What’s a SQL query that tells me time per each step like this: “Download Data” took 25 minutes, “Data Quality Control” took 2 minutes etc.

Cheers.

P.S My RDBMS is MySQL. Is there any way to do this in MySQL ?

  • 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-06-15T14:39:21+00:00Added an answer on June 15, 2026 at 2:39 pm

    You should use datetime or timestamp instead of time to make this work across date boundaries. See comments.

    Standard SQL for most RDBMS

    Use a window function for this. Implemented in most notable RDBMS (except MySQL) by now:

    SELECT *
         ,lead("StartedAt") OVER (ORDER BY "StartedAt") - "StartedAt" AS duration
    FROM  tbl;
    

    lead() retrieves the value of the next row according to the order in the ORDER BY clause. For the last row, where you have no "next" row, you get NULL.

    I quote the manual of PostgreSQL on window functions, since you did not name your RDBMS.

    MySQL

    In the absence of window functions, one way would be to use correlated subqueries:

    SELECT t1.*
         ,(SELECT t2."StartedAt"
           FROM tbl t2
           WHERE t2.id > t1.id
           ORDER BY t2."StartedAt"
           LIMIT 1) - "StartedAt" AS duration
    FROM  tbl t1;
    

    Or this is probably faster:

    SELECT t1."Id", t1."Step", t1."StartedAt"
          ,TIMESTAMPDIFF(MINUTE, t1."StartedAt", min(t2."StartedAt")) AS minutes
    FROM   tbl t1
    LEFT   JOIN tbl t2 ON t2."Id" > t1."Id"
    GROUP  BY t1."Id", t1."Step", t1."StartedAt";
    

    ->sqlfiddle with both queries.

    The manual about TIMESTAMPDIFF() or TIMEPDIFF()

    If your Id column would be ascending without gaps, this would be simpler. But that’s rarely the case in real life tables.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have problem with sql query. For example, I have table like this: ID
I have table like this: +------+-------+ | user | data | +------+-------+ | 1
I have table data like this id id1 name 1 1 test1 1 1
I have a table like this Declare @Temp Table(Data VarChar(20)) Insert Into @Temp Values('F_200_100_')
I have table like this : ID Name_1 Name_2 Name_3 1 Egon Spengler Ives
I have a table like this ID Name IsDeleted 1 Yogesh Null 2 Goldy
I have a table like this : +-------+------------+------+-----+---------+-------+ | Field | Type | Null
I have a table like this: ID country ------------- 1 US 2 Japan 3
I have a table like this: id | id_parent | tag_name 0 | |
I have a table like this: CREATE TABLE book_info ( book_id VARCHAR(32) not null,

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.