Solved

SQL Query - Joined 2 temporary table and align rows

Posted on 2014-02-24
4
292 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
4 Comments
 
LVL 24

Assisted Solution

by:chaau
chaau earned 100 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Awesome.  That was it.
0

Featured Post

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Join & Write a Comment

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

744 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now