I have 3 tables 2 have records that are joined in the 3rd table. tblChangeRequests, tblChangeHistory, tblChangeOrders.
change requests are made and recorded in the change request table. when a change request is a go then change orders are made. they are linked back to the request that created them. sometimes the request can have multiple orders, sometimes the order can fulfill multiple requests, hence the many to many.
the problem is I want to show all the requests and all the orders and not have multiple lines show for each entry in tblChangeHistory. when I put all three in a query I get only the line where all three tables have a record that matches. I need all three to combine into one single "report". I tried to do a subquery for the 2 change tables that linked with the history table. didn't work... :(
Here is a query that I started with:
SELECT
tblChangeHistory.ChangeHistoryID,
tblChangeHistory.ChangeRequestID,
tblChangeHistory.ChangeRequestDate,
0 AS ChangeOrderID,
0 AS ChangeOrderDate,
tblChangeHistory.ChangeHistoryDate,
tblChangeRequestLog.ChangeRequestID AS CRCRID,
tblChangeRequestLog.CRID,
0 AS COCOID,
0 AS COID
FROM tblChangeRequestLog LEFT JOIN tblChangeHistory ON tblChangeRequestLog.ChangeRequestID = tblChangeHistory.ChangeRequestID
UNION SELECT
tblChangeHistory.ChangeHistoryID,
0 AS ChangeRequestID,
0 AS ChangeRequestDate,
tblChangeHistory.ChangeOrderID,
tblChangeHistory.ChangeOrderDate,
tblChangeHistory.ChangeHistoryDate,
0 AS CRCRID,
0 AS CRID,
tblChangeOrdersLog.ChangeOrderID AS COCOID,
tblChangeOrdersLog.COID
FROM tblChangeOrdersLog LEFT JOIN tblChangeHistory ON tblChangeOrdersLog.ChangeOrderID = tblChangeHistory.ChangeOrderID;
That gives me this:
Now as I was putting this together I realized that the reason the one record where all three tables line up are showing on 2 lines is because of the "0 AS ChangeRequestID" and the others where I force a value.
I also tried this as 2 subqueries, but it didn't return the right results either, one record where all match.
Here is what it should look like:
So if anyone can help with returning all records from all tables with one line for matching results it would be much appreciated.