Solved

SQL 2005 - Table Linking

Posted on 2014-10-02
7
141 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
[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
  • 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
Why You Need a DevOps Toolchain

IT needs to deliver services with more agility and velocity. IT must roll out application features and innovations faster to keep up with customer demands, which is where a DevOps toolchain steps in. View the infographic to see why you need a DevOps toolchain.

 
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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
T-sql Date Format 9 67
SQL Query Syntax Assistance 2 44
TSQL remove duplicates from different columns 14 52
Create a Calendar table 29 44
'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

734 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