Question

I have two crosstab Queries that are almost identical one is made to order the other is not made to order (on/off checkbox (criteria on the crosstabs is true or false)) the MTO one has 203 rows and the NMTO one has 160 rows if I left join so I get everything from MTO one I get 213 rows but I need to have 225 rows total however 17 rows only have NMTO data and aren't being included I've tried to rewrite the join using just conditions and am having no luck

I'm probably missing something simple (I hope)

Was it helpful?

Solution

You need to create a "super-set" of the 2 datasets containing the key columns and then use that in the middle of the query using Outer joins

Stage 1: create a UNION query to get the full set of unique keys from MTO and NMTO. This should contain the 225 records which form the superset.

Stage 2: create a second query which includes this UNION query plus the MTO and NMTO. Set the joins to include everything from UNION query and matching records from MTO; do exactly the same for the join between the UNION query and NMTO.

This should now return the 225 records.

==================================================================

EDIT: worked example

Assuming 2 tables (or queries), mto and nmto with a consistent ID field(s) on which to join, the following query would work:

SELECT superset.id, mto.mto_1, mto.mto_2, nmto.nmto_1, nmto.nmto_2
FROM ((SELECT mto.id from mto UNION SELECT nmto.id from nmto)  AS superset 
LEFT JOIN mto ON superset.id = mto.id) 
LEFT JOIN nmto ON superset.id = nmto.id;

You can extend this approach to include more than 2 tables, with the superset at the centre. Any questions please comment below and I will provide a screen shot and/or further information.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top