Solved

SQL Help joining two tables

Posted on 2016-11-09
7
51 Views
Last Modified: 2016-11-10
Good evening. I can do very basic sql syntax. I currently have the following which gives me the result of two tables.

SELECT MH.MO_ID
,MH.BUILD_PART_ID
,MH.BUILD_PART_RVSN_ID
,MH.BLD_PROJ_ID
,MH.BLD_PROJ_ABBRV_CD
,MH.BLD_INVT_ABBRV_CD
,BUILD_PART_KEY
FROM DELTEK.MO_HDR MH
WHERE MO_ID = 'MO-0010720'

SELECT SL.CHG_PROJ_ID
,SL.SO_ID
,SL.SO_LN_NO
,SL.INVT_ABBRV_CD
,SO_LN_KEY
,ITEM_KEY
,ITEM_ID
FROM DELTEK.SO_LN SL
WHERE SO_ID = 'SO-0000954'

One table is sales order header lines and the other is for material orders. I would like to combine these two tables. Essentially the sales order table can have many lines with the same sales order number, there can only be one material order associated with the sales order number. The same would be true for the BLD_PROJ_ID field. I hope that makes sense. I attached a screen shot if that helps.
RESULT.docx
0
Comment
Question by:DJ P
  • 3
  • 3
7 Comments
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 41881380
You'll get the same results for the material order for each line of the sales order but it's doable:
SELECT SL.CHG_PROJ_ID
,SL.SO_ID
,SL.SO_LN_NO
,SL.INVT_ABBRV_CD
,SO_LN_KEY
,ITEM_KEY
,ITEM_ID
,MH.MO_ID
,MH.BUILD_PART_ID
,MH.BUILD_PART_RVSN_ID
,MH.BLD_PROJ_ID
,MH.BLD_PROJ_ABBRV_CD
,MH.BLD_INVT_ABBRV_CD
,BUILD_PART_KEY
FROM DELTEK.SO_LN SL
join DELTEK.MO_HDR MH on SL.CHG_PROJ_ID = MH.bld_proj_id
WHERE SL.SO_ID = 'SO-0000954'

Open in new window

0
 
LVL 28

Expert Comment

by:Pawan Kumar
ID: 41881644
Try. I think Kyle got is right. Appending MH.MO_ID = 'MO-0010720' and InnerJoin. Columns you can add/modify based on your need.

SELECT 
  MH.MO_ID
 ,MH.BUILD_PART_ID MH_BUILD_PART_ID
 ,MH.BUILD_PART_RVSN_ID
 ,MH.BLD_PROJ_ID
 ,MH.BLD_PROJ_ABBRV_CD
 ,MH.BLD_INVT_ABBRV_CD
 ,BUILD_PART_KEY
 ,SL.CHG_PROJ_ID SL_CHG_PART_ID
 ,SL.SO_ID
 ,SL.SO_LN_NO
 ,SL.INVT_ABBRV_CD
 ,SL.SO_LN_KEY
 ,SL.ITEM_KEY
 ,SL.ITEM_ID
 FROM DELTEK.MO_HDR MH
INNER JOIN 
 FROM DELTEK.SO_LN SL
ON SL.CHG_PROJ_ID = MH.BLD_PROJ_ID
WHERE MH.MO_ID = 'MO-0010720' AND SL.SO_ID = 'SO-0000954'

Open in new window

0
 

Author Comment

by:DJ P
ID: 41882011
Pawan,

Just copied and pasted what you have posted and get nothing but errors.
0
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

 
LVL 28

Expert Comment

by:Pawan Kumar
ID: 41882021
Please provide the errors.
0
 

Author Comment

by:DJ P
ID: 41882090
Msg 156, Level 15, State 1, Line 18
Incorrect syntax near the keyword 'FROM'.
0
 
LVL 28

Accepted Solution

by:
Pawan Kumar earned 500 total points
ID: 41882113
Ohh my bad !! Please find the modified query below-

SELECT 
  MH.MO_ID
 ,MH.BUILD_PART_ID MH_BUILD_PART_ID
 ,MH.BUILD_PART_RVSN_ID
 ,MH.BLD_PROJ_ID
 ,MH.BLD_PROJ_ABBRV_CD
 ,MH.BLD_INVT_ABBRV_CD
 ,BUILD_PART_KEY
 ,SL.CHG_PROJ_ID SL_CHG_PART_ID
 ,SL.SO_ID
 ,SL.SO_LN_NO
 ,SL.INVT_ABBRV_CD
 ,SL.SO_LN_KEY
 ,SL.ITEM_KEY
 ,SL.ITEM_ID
 FROM DELTEK.MO_HDR MH
INNER JOIN 
 DELTEK.SO_LN SL
ON SL.CHG_PROJ_ID = MH.BLD_PROJ_ID
WHERE MH.MO_ID = 'MO-0010720' AND SL.SO_ID = 'SO-0000954'

Open in new window

0
 

Author Closing Comment

by:DJ P
ID: 41882599
This is right on the money! Thanks for the assistance.
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

SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

773 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