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 8715745
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T05:55:09+00:00 2026-06-13T05:55:09+00:00

I have table of items, every one has it’s start time and end time.

  • 0

I have table of items, every one has it’s start time and end time.

For example:

| id      | item | start      | end        |
+---------+------+------------+------------+
|    2703 | 223  | 2003-12-30 | 2004-01-16 |
|   47029 | 223  | 2004-04-21 | 2005-01-06 |
| 1386593 | 223  | 2005-04-19 | 2005-04-19 |
| 1386739 | 223  | 2005-04-19 | 2006-12-07 |
| 1432862 | 223  | 2006-02-08 | 2006-02-10 |

I’d like to find gaps between items (days between one’s end with next start). Gap between first two items in example is 93 days, between last 2 there is overlap 302 days, which is counted as zero. So gaps for example table are:

96
103
0
0

My goal is to find average gap (49.75 in this example) or other-words sum of gap days (199).

Optional goals to have: min gap (0) and max gap (103).

My solution for now is iterate over rows and calculate it outside database, but i am looking for prompt solution (inside MySQL). I looked over the aggregate functions but don’t have no idea is this possible or not. How is the best way to find such average gap?

  • 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-13T05:55:10+00:00Added an answer on June 13, 2026 at 5:55 am
    select avg(if (diff < 0, 0, diff))
    from (
        select datediff(min(t2.start), t1.end) as diff
        from Table1 t1
        inner join Table1 t2 on t1.id < t2.id
        group by t1.end
    ) a
    

    SQL Fiddle Example

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

Sidebar

Related Questions

I have a table of items. item has id, score 1, score 2. I
I have a recordset loop that creates a table, and every 9 items it
Hi I have one web service connected to one db that has a table
This one has really got me confused: I have a group of 'items' and
This is a complex one. But I have a table which has a DATETIME
I have a table view with 3 items, one of which I have behind
I have a table Users and a table Items In the Items table, I
I'm working on a very basic shopping cart system. I have a table items
I have a table of items stored this way : A1 | B1 A1
I have a table of items, user should be able to choose some of

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.