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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T13:18:53+00:00 2026-05-13T13:18:53+00:00

Edit 1 (clarification): Thank you for the answers so far! The response is gratifying.

  • 0

Edit 1 (clarification): Thank you for the answers so far! The response is gratifying.
I want to clarify the question a little because based on the answers I think I did not describe one aspect of the problem correctly (and I’m sure that’s my fault as I was having a difficult time defining it even for myself).
Here’s the rub: The result set should contain ONLY the records with tstamp BETWEEN ‘2010-01-03’ AND ‘2010-01-09’, AND the one record where the tstamp IS NULL for each order_num in the first set (there will always be one with null tstamp for each order_num).
The answers given so far appear to include all records for a certain order_num if there are any with tstamp BETWEEN ‘2010-01-03’ AND ‘2010-01-09’. For example, if there were another record with order_num = 2 and tstamp = 2010-01-12 00:00:00 it should not be included in the result.

Original question:
Consider an orders table containing id (unique), order_num, tstamp (a timestamp), and item_id (the single item included in an order). tstamp is null, unless the order has been modified, in which case there is another record with identical order_num and tstamp then contains the timestamp of when the change occurred.

Example…

id  order_num  tstamp               item_id
__  _________  ___________________  _______
 0          1                           100
 1          2                           101
 2          2  2010-01-05 12:34:56      102
 3          3                           113
 4          4                           124
 5          5                           135
 6          5  2010-01-07 01:23:45      136
 7          5  2010-01-07 02:46:00      137
 8          6                           100
 9          6  2010-01-13 08:33:55      105

What is the most efficient SQL statement to retrieve all of the orders (based on order_num) which have been modified one or more times during a certain date range? In other words, for each order we need all of the records with the same order_num (including the one with NULL tstamp), for each order_num WHERE at least one of the order_num’s has tstamp NOT NULL AND tstamp BETWEEN ‘2010-01-03’ AND ‘2010-01-09’. It’s the “WHERE at least one of the order_num’s has tstamp NOT NULL” that I’m having difficulty with.

The result set should look like this:

id  order_num  tstamp               item_id
__  _________  ___________________  _______
 1          2                           101
 2          2  2010-01-05 12:34:56      102
 5          5                           135
 6          5  2010-01-07 01:23:45      136
 7          5  2010-01-07 02:46:00      137

The SQL that I came up with is this, which is essentially “A UNION (B in A)”, but it executes slowly and I hope there is a more efficient solution:

SELECT history_orders.order_id, history_orders.tstamp, history_orders.item_id
FROM
   (SELECT orders.order_id, orders.tstamp, orders.item_id
    FROM orders
    WHERE orders.tstamp BETWEEN '2010-01-03' AND '2010-01-09')
    AS history_orders
UNION
SELECT current_orders.order_id, current_orders.tstamp, current_orders.item_id
FROM
   (SELECT orders.order_id, orders.tstamp, orders.item_id
    FROM orders
    WHERE orders.tstamp IS NULL)
    AS current_orders
WHERE current_orders.order_id IN
   (SELECT orders.order_id
    FROM orders
    WHERE orders.tstamp BETWEEN '2010-01-03' AND '2010-01-09');
  • 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-13T13:18:54+00:00Added an answer on May 13, 2026 at 1:18 pm

    Thank you again for all the suggestions. I found three solutions that work, including my original. At the end I’ve added some performance results, which are not as great as I had hoped. If anyone can improve on this I would be thrilled!

    1) The best solution found so far seems to be:

    SELECT history_orders.order_id, history_orders.tstamp, history_orders.item_id
    FROM
       (SELECT orders.order_id, orders.tstamp, orders.item_id
        FROM orders
        WHERE orders.tstamp BETWEEN '2010-01-03' AND '2010-01-09'
        OR orders.tstamp IS NULL)
        AS history_orders
    WHERE history_orders.order_id IN
       (SELECT orders.order_id
        FROM orders
        WHERE orders.tstamp BETWEEN '2010-01-03' AND '2010-01-09');
    

    2) I also tried using EXISTS in place of IN, which requires an additional WHERE clause in the last SELECT:

    SELECT history_orders.order_id, history_orders.tstamp, history_orders.item_id
    FROM
       (SELECT orders.order_id, orders.tstamp, orders.item_id
        FROM orders
        WHERE orders.tstamp BETWEEN '2010-01-03' AND '2010-01-09'
        OR orders.tstamp IS NULL)
        AS history_orders
    WHERE EXISTS
       (SELECT orders.order_id
        FROM orders
        WHERE history_orders.order_id = orders.order_id
        AND orders.tstamp BETWEEN '2010-01-03' AND '2010-01-09');
    

    3) And finally there is my original solution, using UNION.

    Comments:
    To comment on the table size, my actual “real world” problem involves 4 tables (connected with inner joins) containing 98, 2189, 43897, 785656 records respectively.

    Performance – I ran each solution three times and here are my real world results:
    1: 52, 51, 51 seconds
    2: 54, 54, 53 s
    3: 56, 56, 56 s

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

Sidebar

Related Questions

EDIT: Simple version of the question: I want to create server variables in the
EDIT 07/14 As Bill Burgess mentionned in a comment of his answer, this question
Edit (updated question) I have a simple C program: // it is not important
Probably this is a common question. In fact I think I asked it years
I have found several questions and answers pertaining to my question, but I'm getting
I think this is a stupid question, but I can't quite find the answer
EDIT: I was an idiot. I simply had an image that was vertically long,
EDIT: See my answer below--> I am wanting to have a view that when
EDIT: iam using ajax to load text in my content that is why onload
Edit : Note that, as Daniel and latkin noted in an answer and a

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.