question

TimothyAlvord-8179 avatar image
0 Votes"
TimothyAlvord-8179 asked SimpleSamples commented

UNION, JOIN?

I have a Main table that contains a field 'WONum'. A similarly named filed exists in two other tables. Those two other tables also contain the filed that I want to JOIN or UNION with my main table. That field is named 'WOQty'. So what I need is to JOIN my Main table with the WOQty from either of the other tables. Is that a JOIN or a UNION or something else entirely?

sql-server-general
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

TimothyAlvord-8179 avatar image
0 Votes"
TimothyAlvord-8179 answered SimpleSamples commented

Scott from another forum helped me with this query:

SELECT mt.BusinessUnit, mt.PartNum, mt.WONum, mt.TransDate, mt.TransQty, ISNULL(ot1.WOQty, ot2.WOQty) AS WOQty

FROM tblJDEWOData2 AS mt LEFT OUTER JOIN

tblWONumWOQty AS ot1 ON ot1.WONum = mt.WONum LEFT OUTER JOIN

tblHistory AS ot2 ON ot1.WONum IS NULL AND ot2.WONum = mt.WONum

ORDER BY mt.TransDate

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

People that volunteer their time to try to help prefer that people requesting help not post the same question multiple places at the same time. Did my answer help at all? If it did but you consider the other forum to be the answer then that is why it would have been beneficial for the both of us that you not post the same question multiple places at the same time.

0 Votes 0 ·
SimpleSamples avatar image
0 Votes"
SimpleSamples answered

Do you want to combine rows or columns?

Do you want the result to have some rows from one table and some rows from another? If so then you want to do a union.

Do you want the result to have some columns from one table and some columns from another? If so then you want to do a join.

See SQL Server UNION: The Ultimate Guide for further explanation.


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.