UNION, JOIN?

Timothy Alvord 211 Reputation points
2021-07-01T17:04:07.62+00:00

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
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
12,758 questions
0 comments No comments
{count} votes

Accepted answer
  1. Timothy Alvord 211 Reputation points
    2021-07-01T19:57:17.367+00:00

    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 additional answer

Sort by: Most helpful
  1. Sam of Simple Samples 5,516 Reputation points
    2021-07-01T17:55:16.077+00:00

    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.

    0 comments No comments