Solved

SQL Query - Joined 2 temporary table and align rows

Posted on 2014-02-24
4
314 Views
Last Modified: 2014-03-05
Not sure how to explain this, but will try my best and hopefully someone can assist.  I have 2 SQL queries that query separate tables.  The 2 queries have no relations (yet tied together) and is base of a material planning sort by date.

Here's example of my raw data and final output.  The raw data shows from first query pulling (Want Date, Order#, and Qty).  The 2nd query pull fields "Required Date and Job#".

Sort and Align by Want_date and Required_date
With the above, if I sort by Want Date and Required Date, it aligns one after another.  However, I need to align the rows with "Want Date" closes to or equal to the "Required Date".

Example, line 2 of my Raw Data has a "Want Date" of 1/15/2014, and closes "Required Date" is 1/20/2014.  Since it is equal or greater than line 3, I want to align it with line 2.  See Final output.  Is this even doable?
0
Comment
Question by:holemania
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 25

Assisted Solution

by:chaau
chaau earned 100 total points
ID: 39884312
This query should do the trick:
select * from tab1 t1
left join tab2 t2 on t1.wantdate <= t2.requireddate 
  and t1.wantdate = (select max(wantdate) from tab1 where tab1.wantdate <= t2.requireddate)

Open in new window

SQL Fiddle
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 39884316
I'd suggest providing the sql queries you are using right now, plus sample data from the source tables (not the temp tables)
0
 
LVL 34

Accepted Solution

by:
Brian Crowe earned 400 total points
ID: 39884413
CREATE TABLE #Order
(
      OrderID                  INT,
      WantDate            DATETIME,
      Qty                        INT
);

CREATE TABLE #Job
(
      JobID                  INT,
      RequiredDate      DATETIME
);

INSERT INTO #Order (OrderID, WantDate, Qty)
VALUES (29933, '1/1/2014', 2),
      (299335, '1/15/2014', 1),
      (29940, '1/25/2014', 2),
      (29930, '1/30/2014', 1),
      (29935, '2/14/2014', 2),
      (29936, '2/15/2014', 1)

INSERT INTO #Job (JobID, RequiredDate)
VALUES (29932, '1/20/2014'),
      (29933, '1/25/2014'),
      (29930, '2/27/2014')

WITH cte
AS
(
      SELECT OrderID, WantDate, Qty,
            ROW_NUMBER() OVER(ORDER BY WantDate) AS RowNumber
      FROM #Order
)
SELECT O.WantDate, O.OrderID AS [Order#], O.Qty, J.RequiredDate, J.JobID AS [Job#]
FROM cte AS O
LEFT OUTER JOIN cte AS ONext
      ON O.RowNumber = ONext.RowNumber - 1
LEFT OUTER JOIN #Job AS J
      ON J.RequiredDate >= O.WantDate
      AND (ONext.WantDate IS NULL OR J.RequiredDate < ONext.WantDate)
0
 

Author Closing Comment

by:holemania
ID: 39907081
Awesome.  That was it.
0

Featured Post

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

735 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question