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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T11:45:18+00:00 2026-06-05T11:45:18+00:00

I’m having some trouble with a complex query involving the following tables. Assume time

  • 0

I’m having some trouble with a complex query involving the following tables. Assume time is using the built-in sqlite timestamp datatype.

I am trying to return the customers whose 2nd purchase is within 4 hours of their first purchase AND if it’s within 2 hours it must be from a different store.

I’m having trouble wrapping my head around how to refer to the specific rows to compare a first purchase with a second purchase.

purchases

purchase_id | customer_id | store_id | purchase_time
     1             1            1      2009-01-27 10:00:00.0
     2             1            2      2009-01-27 10:30:00.0
     3             2            1      2009-01-27 10:00:00.0
     4             2            1      2009-01-27 10:30:00.0
     5             3            1      2009-01-27 10:00:00.0
     6             3            2      2009-01-27 16:00:00.0
     7             4            3      2009-01-27 10:00:00.0
     8             4            3      2009-01-27 13:00:00.0

stores

store_id | misc columns...
    1
    2
    3

customers

customer_id | f_name
     1         name1
     2         name2
     3         name3
     4         name4

The correct return would be name1, name4 in this case.

  • 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-05T11:45:19+00:00Added an answer on June 5, 2026 at 11:45 am

    You’re going to be joining the purchase table to itself, and then selecting on one of the two criteria.

    The only real trick here is to formulate the different time criteria as:

    1. Purchases that were made < 2 hours at different stores.
    2. Purchases that were made between 2 and 4 hours, independent of store_id.

    Both of which obviously apply for the same customer_id.

    So, we’ve got:

    select p1.purchase_id purchase_1, 
           p2.purchase_id purchase_2,
           c.name,
           p1.customer_id customer
    from purchases p1
    join purchases p2 on
    p1.customer_id = p2.customer_id
    join customer c on c.customer_id = p1.customer_id
    where p1.purchase_time < p2.purchase_time
     and (
            (
            addtime(p1.purchase_time,'2:00:00') >= p2.purchase_time
            and p1.store_id <> p2.store_id
            )
          or
            (
            addtime(p1.purchase_time,'2:00:00') < p2.purchase_time
            and addtime(p1.purchase_time,'4:00:00') >= p2.purchase_time
            )
         )
    

    Which joins purchases to itself by customer_id, first checks that you’re comparing earlier purchases to later purchases, and then applies the two different criteria in the criteria that are ORed.

    I find the time comparisons easiest to do with the addtime() and then comparing the results. Others may prefer other ways.

    SQL Fiddle here: http://sqlfiddle.com/#!2/14dda/2

    Results:

    PURCHASE_1  PURCHASE_2        NAME        CUSTOMER
    1           2                 name1       1
    7           8                 name4       4
    

    —

    EDIT: Perhaps, you’d get some efficiency by moving the p1.purchase_time < p2.purchase_time up into the join clause. This might be faster with lots of data, though the execution plans for this little amount of data are identical. You’d like the optimizer to eliminate all those cases where p1.purchase_time > p2.purchase_time before doing the more expensive comparisons. But that’s somewhat beyond the basic question of ways to write this query.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
We're building an app, our first using Rails 3, and we're having to build
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

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.