SQL Joining trouble

104 views Asked by At

I have 5 differents select that I run to get data from each of them. Each has WHERE and AND clauses on them. Is there a way that I can take all of these pieces of data and put them in one query without messing the data up?

Building SQL:

SELECT 

C1.Registration_No AS 'Information Provider GLN',
C1.Note AS 'ManufacturerID (IPC assigned)',
C1.Name AS 'Manufacturer Name',
C1.Old_Building_Code AS 'Physical GLN',
C1.Additional_Note AS 'Location ID (IPC assigned)',
C1.Building_Code AS 'Location ID (Supplier)',
CONCAT(C1.Name,C2.Address1,C2.City,C2.State) AS 'Location Name'

FROM Common_V_Building AS C1

JOIN Common_V_Address As C2
ON C2.PCN = C1.Plexus_Customer_No
AND C2.Address_Key = C1.Address_Key

WHERE C1.Additional_Note IN ('SWL061', 'SPL139')

Production Qty Code:

SELECT 

P1.Other_Note,
P1.Part_No,
P4.UPC_Code,
P1.Name,
SUM(P5.Quantity) AS 'Production Qty'

FROM Part_V_Part As P1

JOIN Part_V_Part_Attributes AS P4
ON P4.PCN = P1.Plexus_Customer_No
AND P4.Part_Key = P1.Part_Key

JOIN Part_V_Production AS P5
ON P5.Plexus_Customer_No = P1.Plexus_Customer_No
AND P5.Part_Key = P1.Part_Key

WHERE P1.Part_No IN ('07809B', '07810T', '07810K', '07811T', '07811TK', '07812B', '05850T', '05853T')
AND P5.Report_DATE >= '12-21-2016'
AND P5.Report_DATE <= '12-21-2016'

GROUP BY P1.Part_No, P1.Other_Note, P4.UPC_Code, P1.Name, P1.Plexus_Customer_No, P1.Building_Key

Inventory Qty Code:

SELECT 

P1.Plexus_Customer_No,
P2.Part_No,
SUM(P1.QUANTITY) AS 'Inventory (after shipments)'

FROM PART_V_CONTAINER AS P1

JOIN PART_V_PART AS P2
ON P2.Plexus_Customer_No = P1.Plexus_Customer_No
AND P2.Part_Key = P1.Part_Key

WHERE ACTIVE = '1'
AND PART_NO IN ('07809B', '07810T', '07810K', '07811T', '07811TK', '07812B',     '05850T', '05853T')

GROUP BY P2.Part_No, P1.Plexus_Customer_No

Shipment Qty Code:

SELECT

SP1.PCN,
SP4.Part_No,
SUM(SP3.Quantity) As 'Shipments'

FROM SALES_V_SHIPPER AS SP1

JOIN SALES_V_SHIPPER_LINE AS SP2
ON SP2.PCN = SP1.PCN
AND SP2.SHIPPER_KEY = SP1.SHIPPER_KEY

JOIN SALES_V_SHIPPER_CONTAINER AS SP3
ON SP3.PCN = SP2.PCN
AND SP3.SHIPPER_LINE_KEY = SP2.SHIPPER_LINE_KEY

JOIN PART_V_PART AS SP4
ON SP4.PLEXUS_CUSTOMER_NO = SP2.PCN
AND SP4.PART_KEY = SP2.PART_KEY

WHERE SP4.Part_No IN ('07809B', '07810T', '07810K', '07811T', '07811TK', '07812B', '05850T', '05853T')
AND SP3.Loaded_DATE >= '12-21-2016'
AND SP3.Loaded_DATE <= '12-22-2016'

GROUP BY SP4.Part_NO, SP1.PCN

Commited Qty Code:

SELECT

PO1.PCN,
PO3.PART_NO,
SUM(PO2.QUANTITY) AS 'Committed'

FROM SALES_V_PO_LINE AS PO1

JOIN SALES_V_RELEASE AS PO2
ON PO2.PCN = PO1.PCN
AND PO2.PO_LINE_KEY = PO1.PO_LINE_KEY

JOIN PART_V_PART AS PO3
ON PO3.PLEXUS_CUSTOMER_NO = PO1.PCN
AND PO3.PART_KEY = PO1.PART_KEY

JOIN SALES_V_PO AS PO4
ON PO4.PCN = PO1.PCN
AND PO4.PO_KEY = PO1.PO_KEY

JOIN SALES_V_PO_STATUS AS PO5
ON PO5.PCN = PO4.PCN
AND PO5.PO_STATUS_KEY = PO4.PO_STATUS_KEY

WHERE PO5.PO_STATUS = 'OPEN'
AND PO3.PART_NO IN ('07809B', '07810T', '07810K', '07811T', '07811TK', '07812B', '05850T', '05853T')

GROUP BY PO3.PART_NO, PO1.PCN

What I want is :

Record_Date,
Registration_No AS 'Information Provider GLN',
Note AS 'ManufacturerID (IPC assigned)',
Name AS 'Manufacturer Name',
Old_Building_Code AS 'Physical GLN',
Additional_Note AS 'Location ID (IPC assigned)',
Building_Code AS 'Location ID (Supplier)',
CONCAT(Name,Address1,City,State) AS 'Location Name',
Other_Note AS 'Product Number (SO#/CO#)',
Part_No AS 'MPN',
UPC_Code AS 'GTIN',
Name AS 'Product Description',
GETDATE() AS 'Inventory Reported For Date',
'8:30 AM' AS 'Submission Time',
SUM(Quantity) AS 'Production Qty',
SUM(QUANTITY) AS 'Shipments',
SUM(Quantity) AS 'Inventory (After Shipments)',
SUM(Quantity) AS 'Committed'

I am working in an SQL development Environment inside of PLEX if that might help.

Thanks, let me know if you need any more information.

1

There are 1 answers

0
gbeaven On

I used to do work in Plex ERP but no longer have access. You'll need to search for their database schema, which if I remember correctly, doing a search (ctrl+m) "%database%table%" (or something similar) will return you the screen that you need to look at. You'll then need to look for primary/secondary relationships among the tables you've provided us. If you can at least give us high level information for each table, I'm sure someone can provide a better answer.