Solved

SQL Help joining two tables

Posted on 2016-11-09
7
70 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
[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
  • 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 29

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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
LVL 29

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 29

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

615 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