?
Solved

SQL 2005 - Table Linking

Posted on 2014-10-02
7
Medium Priority
?
148 Views
Last Modified: 2014-10-03
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.
0
Comment
Question by:holemania
  • 3
  • 2
  • 2
7 Comments
 
LVL 22

Expert Comment

by:plusone3055
ID: 40357279
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
 
LVL 22

Expert Comment

by:plusone3055
ID: 40357280
give me a few to write the statement
0
 
LVL 22

Expert Comment

by:plusone3055
ID: 40357313
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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 49

Accepted Solution

by:
PortletPaul earned 2000 total points
ID: 40358751
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
 

Author Comment

by:holemania
ID: 40359861
Wow you are awesome PortletPaul.  That's exactly what I was after.  Thanks!!!
0
 

Author Closing Comment

by:holemania
ID: 40359863
Awesome.  That's what I was looking for and that helps tremendously.
0
 
LVL 49

Expert Comment

by:PortletPaul
ID: 40360557
Thank you, all the best. Cheers, Paul
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…

578 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