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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T19:09:34+00:00 2026-05-17T19:09:34+00:00

I have data in an SQLite table that looks like this: user_id event_date ———-

  • 0

I have data in an SQLite table that looks like this:

user_id     event_date
----------  ----------
1000001     2008-01-01
1000001     2008-03-13
1000001     2008-07-04
1000002     2007-01-06
1000002     2008-01-01
1000002     2009-06-01
1000002     2010-12-11

For each of the user_ids I would like to select the largest time gap between pairs of consecutive event_dates. For example in this particular data, user 1000001 has two gaps: 72 days between 2008-01-01 and 2008-03-13, and 113 days between 2008-03-13 and 2008-07-04, so the query should output 113 for user 1000001. User 1000002 has three gaps; largest is 558 days.

Is this possible? Would I have to pre-calculate the time spans and store them with the data, or select everything and post process? I’d like to be able to get it done directly with only the data as shown and directly in the database. Am I abusing SQL by expecting it to be able to treat this data as a list?
Thanks.

  • 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-17T19:09:35+00:00Added an answer on May 17, 2026 at 7:09 pm

    If you can not change the data-model, you could use:

    SELECT user_id, MAX(event_date - prior_event_date)
      FROM (SELECT t.user_id, t.event_date, MAX(prior.event_date) AS prior_event_date
              FROM your_table AS t
                   JOIN your_table AS prior ON (t.user_id=prior.user_id
                                                AND prior.event_date < t.event_date)
             GROUP BY t.user_id, t.event_date) AS events
     GROUP BY user_id;
    

    If you want 0s to be included use a LEFT JOIN.

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

Sidebar

Related Questions

I have data that looks like this: entities id name 1 Apple 2 Orange
I have data that looks like CUSTOMER, CUSTOMER_ID, PRODUCT ABC INC 1 XYX ABC
I have an sqlite table that I'd like to add a new field to.
I have a UITableView that's populated using core data & sqlite. I'd like to
I have data from a table in a database (string) that contain text and
If I have data like this: Key Name 1 Dan 2 Tom 3 Jon
I have a script that parses xml file to generate SQLite table automatically. And,
I have data in this form, Article ID Company A Company B Company C
I have data that needs to be executed on a certain background thread. I
I often have data in Excel or text that I need to get into

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.