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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T08:43:24+00:00 2026-05-29T08:43:24+00:00

I created two temp tables, one table is for orders and the other is

  • 0

I created two temp tables, one table is for orders and the other is for re-orders within 30 days from the orders temp table. I would like to see the original order and any re-order within 30 days. I though using two temp tables would work.

The first temp table called #orders has all of the orders for the past year.
#orders
Fields: RecordID (text), order date, ship date

For the second table I used the dateadd function to get any order within 30 days of the shipddate above, it has the same fields (#reorder)

Select recorded, orderdate, shipdate
Into #reorder
Where shipdate between dateadd(day, 1, p.eff_to) and dateadd(day, 30,p.eff_to)

My query to get the original order and any reorders (to exclude any orders that did not have a reorder within 30 days by joining the two tables is:

Select o.recordid, o.orderdate, o.shipdate, r.recordid, r.orderdate, r.shipdate
From #orders o
Left join #reorder 
Where o.recordid = r.recordid and o.shipdate between dateadd(day, -30,orderdate)    
and dateadd(day, -1, r.orderdate)

I tested some of the results and I am either not getting all of the records returned or redundant records returned.

Sample data 1:

#orders     
RecordID    Orderdate   ShipDate
525     1/8/2011        1/10/2011
525     3/22/2011       3/23/2011
525     4/5/2011        4/6/2011
525     4/12/2011       6/4/2011


#reorder    Orderdate   ShipDate
525     4/5/2011        4/6/2011
525     4/12/2011       6/4/2011


Results:    Orderdate   ShipDate
 525    3/22/2011   3/23/2011
 525    3/22/2011   3/23/2011
 525    4/5/2011    4/6/2011

expected results    
Orderdate   ShipDate
525     3/22/2011       3/23/2011
525     4/5/2011        4/6/2011
525     4/12/2011       6/4/2011

Sample #2:

#orders     
RecordID    Orderdate   ShipDate
101     2/22/2011       3/3/2011
101     5/22/2011       6/6/2011
101     6/27/2011       7/8/2011
101     7/11/2011       7/19/2011
101     11/25/2011      12/1/2011

#reorder    Orderdate   ShipDate
101      6/27/2011      7/8/2011
101      7/11/2011      7/19/2011


Results:    Orderdate   ShipDate
101     6/27/2011       7/8/2011
101     7/11/2011       7/19/2011


Expected results:   
Orderdate   ShipDate
101     5/22/2011       6/6/2011
101     6/27/2011       7/8/2011
101     7/11/2011       7/19/2011

tsql

  • 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-29T08:43:25+00:00Added an answer on May 29, 2026 at 8:43 am

    My query to get the original order and any reorders (to exclude any
    orders that did not have a reorder within 30 days by joining the two
    tables is:

    Sample – Expected Result – 3 should not come up in resultset => Because it violates the above statement as it is not coming in 30 days.

    You can check the query below.

    create table #orders
    (
        RecordID int,
        orderdate datetime, 
        shipdate datetime
    )
    
    create table #reorder
    (
        Reorder int,
        orderdate datetime, 
        shipdate datetime
    )
    insert into #orders(RecordID,orderdate, shipdate)
    values(525, '1/8/2011', '1/10/2011')
    
    insert into #orders(RecordID,orderdate, shipdate)
    values(525, '3/22/2011', '3/23/2011')
    
    
    insert into #orders(RecordID,orderdate, shipdate)
    values(525, '4/5/2011', '4/6/2011')
    
    
    insert into #orders(RecordID,orderdate, shipdate)
    values(525, '4/12/2011', '6/4/2011')
    
    
    insert into #reorder(Reorder,orderdate, shipdate)
    values(525, '4/5/2011', '4/6/2011')
    
    
    insert into #reorder(Reorder,orderdate, shipdate)
    values(525, '4/12/2011', '6/4/2011')
    
    Select o.recordid, o.orderdate, o.shipdate, r.Reorder, 
    r.orderdate, r.shipdate
    From #orders o
    Inner join #reorder r on o.recordid = r.Reorder
    Where  r.shipdate between dateadd(day, -1, o.orderdate)    
    and dateadd(day, 30,o.orderdate)
    
    drop table #orders
    drop table #reorder
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Two temp tables are created and then loaded...Here's the schema. Create table #SH ([date]
I have created two JavaScript files. One file is "validators.js" and the other is
Within PHP I do: 1.) A temporary table is created: CREATE TEMP TABLE new_table
I have two tables. One is a table of the reports that have been
I have two tables and I'm looking for the rows in one table where
Iv got two DB tables. One containing types(Id, Name) and the other holds datapoints
I created two tables: Publisher PK, PublisherId, int, not null, indentity +1 PublisherName, varchar(50),
I've created two classes, with one of them having an implicit cast between them:
I have created two forms in my Windows Application. One Form acts as a
I have two IDbCommand objects that are created from an NHibernate session, and they

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.