Solved

SQL 2005 - Table Linking

Posted on 2014-10-02
7
137 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
sql help 8 55
Union 2 queries to a cte (temp table perhaps) 9 36
SQL Server 2012 rs - Field1 and Field2 add 1 to Total 4 18
Problem with duplicate records in Oracle query 16 26
Introduction Hopefully the following mnemonic and, ultimately, the acronym it represents is common place to all those reading: Please Excuse My Dear Aunt Sally (PEMDAS). Briefly, though, PEMDAS is used to signify the order of operations (http://en.…
In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

770 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