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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T17:57:46+00:00 2026-06-08T17:57:46+00:00

Thanks for looking. I am trying to get the smallest time between visits per

  • 0

Thanks for looking.

I am trying to get the smallest time between visits per customer. With the below code I am getting the amount of time between each visit, but it is comparing every date. I just need the smallest margin and then to get rid of/hide all the others.

select a.account_id, a.transaction_id, b.transaction_date , a.transaction_date,            round(a.transaction_date - b.transaction_date, 0) as Time_between
from time_between_trans a, time_between_trans b
where a.transaction_date > b.transaction_date
and a.account_ID like '717724'
and a.account_id = b.account_id
  • for each date in transaction_date find the closest date to it in the
    trasactions

  • They must be of the same account_id and the second date must be later
    than the first

  • 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-08T17:57:47+00:00Added an answer on June 8, 2026 at 5:57 pm

    You need to add a group by to your query:

    select a.account_id, a.transaction_id, min(time_between) as min_time_between
    from (select a.account_id, a.transaction_id, b.transaction_date, a.transaction_date,
                 round(a.transaction_date - b.transaction_date, 0) as Time_between
          from time_between_trans a join
               time_between_trans b
               on a.transaction_date > b.transaction_date and
                  a.account_ID ='717724' and
                  a.account_id = b.account_id
         ) a
    group by a.account_id, a.transaction_id
    

    I also fixed your join syntax to use proper join syntax, and changed the “like” to an “=”, since you have no wildcards.

    There may be other methods to express this, but this is standard SQL. You should specify the database you are using in your question.

    If you are using Oracle, then just do the following:

    select a.*,
           (case when nextdt - transaction_date) < transaction_date - nextdt
                 then nextdt - transaction_date)
                 else transaction_date - nextdt
            end) as mintime
    from (select a.*,
                 lead(transaction_date, 1) over (partition by account_id order by transaction_date) as nexttd,
                 lag(transaction_date, 1) over (partition by account_id order by transaction_date) as prevtd
          from time_between_trans a
         ) a
    

    Actually, this isn’t quite correct, because you have to take NULLs into account for nexttd and prevtd. But, the idea is simple. The lead and lag functions let you take the prev or text transaction, and then you can do whatever you want to find the minimum — or get whatever other information you would like from the records.

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

Sidebar

Related Questions

thanks for looking, I've had a terrible time trying to get the right search
I'm currently trying to get the amount of appearences of each word in a
I'm a long time C++/Java developer trying to get into Python and am looking
Thanks for looking, I'm trying to get 20 entries from the database randomly and
I'm looking at CocoaLibSpotify, and trying to get a simple search request working. I
I am trying to get a stringArray resource with this code: String continent =
We are trying to get part of a API Working and we are getting
Trying to: get all users by using the query in the below script into
I'm trying to get the individual values of a rgb string. I've been getting
I'm a sometime developer who is trying to get up to speed on customer-facing

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.