SQL 2005 - Table Linking

I have 2 sql database tables, DEMAND and ORDER.  The table "Demand" has what's in demand with dates for items that need to be ordered.  The "Demand" table is kind of like an MRP table.  The "Order" table, is the purchase order table on materials being ordered.

Demand Table
Demand table
Order Table
Order Table
I need to be able to join these 2 tables together, but only have it shown on the line that needs it.  With the "Example1", the join is base off the "Item ID", but to have it shown in the correct line is base off the "Qty On Hand".  The "Example1" diagram shows 0 on hand, so it's going to be needed on the first line.  Now it runs out on Job ID 4715, so next job I need to align the next Order line with that line.  

Example1
Example1
The example2, qty on hand has 3 on hand.  So next time it run out will be on job 4715, so I need to have the first order item align with 4720 since those will be ordered and received.  I then run out with that first order on job 4725, then I need to align the next order with job 4726.

Example2
Example2
I'm not even sure how to begin this so that I can align the right sales order to the demand line when it's needed per my example.  Can such thing be done?  If I join it by part ID, I have my demand repeating twice since I have 2 orders open, and they do not align when I need it.  I need to align my demand line with the next order when it's needed per the above example.
holemaniaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

plusone3055Commented:
you can accomplish this with a case statement
you can join on the item ID

and do a case when qty on hand < min required
0
plusone3055Commented:
give me a few to write the statement
0
plusone3055Commented:
Actaully here is an easier way with jsut a JOIN
SELECT  D.DateRequired
	   ,D.QtyRequired
	   ,D.JobID
	   ,D.ItemID
	   ,D.QtyOnHand
	   ,O.Duedate
	   ,O.QtyDue
	   ,O.OrderID
	   
FROM 	Demand D 
JOIN   Order O 
ON     D.ItemID = O.ItemID

Open in new window

0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

PortletPaulfreelancerCommented:
Oh, please don't name a table "Order", that is a reserved SQL keyword and you will have an awful time.
I have used [Orders] instead.

There's more to it than just a join or a case expression.

"running sums" are required of both demand and supply (and this isn't simple prior to SQL 2012)
I have assume SQL Server 2005 due to the title & topic chosen (technique used will work in all versions).

These are the results I obtained:
Test 1 (zero qty on hand)
| RUNNING_DEMAND | RUNNING_SUPPLY |   DUE_DATE | QTY_DUE | ORDER_ID | ITEM_ID |   DATE_REQ | JOB_ID | QTY_ON_HAND | QTY_REQD |
|----------------|----------------|------------|---------|----------|---------|------------|--------|-------------|----------|
|              1 |         (null) |     (null) |  (null) |   (null) |  (null) | 2014-10-03 |   4712 |           0 |        1 |
|              2 |         (null) |     (null) |  (null) |   (null) |  (null) | 2014-10-04 |   4713 |           0 |        1 |
|              3 |              3 | 2014-10-09 |       3 |    29822 |  ABC123 | 2014-10-05 |   4714 |           0 |        1 |
|              4 |         (null) |     (null) |  (null) |   (null) |  (null) | 2014-10-06 |   4715 |           0 |        1 |
|              5 |         (null) |     (null) |  (null) |   (null) |  (null) | 2014-10-07 |   4716 |           0 |        1 |
|              6 |         (null) |     (null) |  (null) |   (null) |  (null) | 2014-10-08 |   4717 |           0 |        1 |
|              7 |              7 | 2014-10-10 |       4 |    29833 |  ABC123 | 2014-10-09 |   4718 |           0 |        1 |
|              8 |         (null) |     (null) |  (null) |   (null) |  (null) | 2014-10-10 |   4719 |           0 |        1 |
|              9 |         (null) |     (null) |  (null) |   (null) |  (null) | 2014-10-11 |   4720 |           0 |        1 |
|             10 |         (null) |     (null) |  (null) |   (null) |  (null) | 2014-10-12 |   4721 |           0 |        1 |
|             11 |         (null) |     (null) |  (null) |   (null) |  (null) | 2014-10-13 |   4722 |           0 |        1 |
|             12 |         (null) |     (null) |  (null) |   (null) |  (null) | 2014-10-14 |   4723 |           0 |        1 |
|             13 |         (null) |     (null) |  (null) |   (null) |  (null) | 2014-10-15 |   4724 |           0 |        1 |

Open in new window

Test 2 (3 qty on hand)
| RUNNING_DEMAND | RUNNING_SUPPLY |   DUE_DATE | QTY_DUE | ORDER_ID | ITEM_ID |   DATE_REQ | JOB_ID | QTY_ON_HAND | QTY_REQD |
|----------------|----------------|------------|---------|----------|---------|------------|--------|-------------|----------|
|             -2 |         (null) |     (null) |  (null) |   (null) |  (null) | 2014-10-03 |   4712 |           3 |        1 |
|             -1 |         (null) |     (null) |  (null) |   (null) |  (null) | 2014-10-04 |   4713 |           3 |        1 |
|              0 |         (null) |     (null) |  (null) |   (null) |  (null) | 2014-10-05 |   4714 |           3 |        1 |
|              1 |         (null) |     (null) |  (null) |   (null) |  (null) | 2014-10-06 |   4715 |           3 |        1 |
|              2 |         (null) |     (null) |  (null) |   (null) |  (null) | 2014-10-07 |   4716 |           3 |        1 |
|              3 |              3 | 2014-10-09 |       3 |    29822 |  ABC123 | 2014-10-08 |   4717 |           3 |        1 |
|              4 |         (null) |     (null) |  (null) |   (null) |  (null) | 2014-10-09 |   4718 |           3 |        1 |
|              5 |         (null) |     (null) |  (null) |   (null) |  (null) | 2014-10-10 |   4719 |           3 |        1 |
|              6 |         (null) |     (null) |  (null) |   (null) |  (null) | 2014-10-11 |   4720 |           3 |        1 |
|              7 |              7 | 2014-10-10 |       4 |    29833 |  ABC123 | 2014-10-12 |   4721 |           3 |        1 |
|              8 |         (null) |     (null) |  (null) |   (null) |  (null) | 2014-10-13 |   4722 |           3 |        1 |
|              9 |         (null) |     (null) |  (null) |   (null) |  (null) | 2014-10-14 |   4723 |           3 |        1 |
|             10 |         (null) |     (null) |  (null) |   (null) |  (null) | 2014-10-15 |   4724 |           3 |        1 |

Open in new window


The query for these is:
select
      d.running_demand
    , o.running_supply
    , o.Due_date
    , o.Qty_Due
    , o.Order_ID
    , o.Item_ID
    , d.Date_Req
    , d.JOB_id
    , d.Item_id
    , d.Qty_on_hand
    , d.Qty_Reqd
from (
      select
            d.Date_Req
          , d.JOB_id
          , d.Item_id
          , d.Qty_on_hand
          , d.Qty_Reqd
          , sum(d.Qty_Reqd) - d.Qty_on_hand AS running_demand
      from demand d
      left join demand d2 on d.Item_id = d2.Item_id and d.Date_Req >= d2.Date_Req
      group by 
            d.Date_Req
          , d.JOB_id
          , d.Item_id
          , d.Qty_on_hand
          , d.Qty_Reqd
     ) d
left join (
            select
                    o.Due_date
                  , o.Qty_Due
                  , o.Order_ID
                  , o.Item_ID
                  , SUM(o2.Qty_Due) as running_supply
            from Orders o
            left join Orders o2 on o.Item_ID = o2.Item_ID and o.Due_date >= o2.Due_date
            GROUP BY
                      o.Due_date
                  , o.Qty_Due
                  , o.Order_ID
                  , o.Item_ID
  ) o on d.item_id = o.item_id and d.running_demand = o.running_supply
;

Open in new window


how I got there:
CREATE TABLE Demand
	([Date_Req] date, [Qty_Reqd] int, [JOB_id] int, [Item_id] varchar(6), [Qty_on_hand] int)
;
	
INSERT INTO Demand
	([Date_Req], [Qty_Reqd], [JOB_id], [Item_id], [Qty_on_hand])
VALUES
	('2014-10-03 00:00:00', 1, 4712, 'ABC123', 0),
	('2014-10-04 00:00:00', 1, 4713, 'ABC123', 0),
	('2014-10-05 00:00:00', 1, 4714, 'ABC123', 0),
	('2014-10-06 00:00:00', 1, 4715, 'ABC123', 0),
	('2014-10-07 00:00:00', 1, 4716, 'ABC123', 0),
	('2014-10-08 00:00:00', 1, 4717, 'ABC123', 0),
	('2014-10-09 00:00:00', 1, 4718, 'ABC123', 0),
	('2014-10-10 00:00:00', 1, 4719, 'ABC123', 0),
	('2014-10-11 00:00:00', 1, 4720, 'ABC123', 0),
	('2014-10-12 00:00:00', 1, 4721, 'ABC123', 0),
	('2014-10-13 00:00:00', 1, 4722, 'ABC123', 0),
	('2014-10-14 00:00:00', 1, 4723, 'ABC123', 0),
	('2014-10-15 00:00:00', 1, 4724, 'ABC123', 0)
;

CREATE TABLE Orders
	([Due_date] date, [Qty_Due] int, [Order_ID] int, [Item_ID] varchar(6))
;
	
INSERT INTO Orders
	([Due_date], [Qty_Due], [Order_ID], [Item_ID])
VALUES
	('2014-10-09 00:00:00', 3, 29822, 'ABC123'),
	('2014-10-10 00:00:00', 4, 29833, 'ABC123')
;

http://sqlfiddle.com/#!3/43dd9/2

Open in new window


You will need indexes on ALL fields used in the joins!
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
holemaniaAuthor Commented:
Wow you are awesome PortletPaul.  That's exactly what I was after.  Thanks!!!
0
holemaniaAuthor Commented:
Awesome.  That's what I was looking for and that helps tremendously.
0
PortletPaulfreelancerCommented:
Thank you, all the best. Cheers, Paul
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.