Solved

SQL 2005 - Table Linking

Posted on 2014-10-02
7
127 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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 48

Accepted Solution

by:
PortletPaul earned 500 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 48

Expert Comment

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

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

757 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

19 Experts available now in Live!

Get 1:1 Help Now