Question : Filte removing records from query?

I have a left join query that I'm trying to filter with parameters for the date range.  The tables are joined by Customer ID.  I'm pulling a field from two tables both month fields.  I'm trying to get ALL of the records from the left table within a date range and those matching the right table.  I'm currently linking by the customer ID, with a parameter filter for begin and end date for both month fields (one for each table).  

I think I just answered my own question (I need to create a unique ID field based on the Customer ID and month), but I'd like to unde15rstand why doing it the current way doesn't work.

Answer : Filte removing records from query?

If you are going to do this in design view, you should first select TableA, and all the fields you need from it for the subquery.  Also plug in your criteria.

Then switch to SQL view and change:

SELECT Field1, Field2, Field3
FROM TableA
WHERE TableA.DateField BETWEEN #1/1/2010# and #2/14/2010

to read:

SELECT CustomerID, Field2, Field3
FROM (
SELECT CustomerID, Field2, Field3
FROM TableA
WHERE TableA.DateField BETWEEN #1/1/2010# and #2/14/2010) as A

Then switch back to the Query design view.  You will now see what looks like a table, with the label A.  Add TableB to the query design grid, connect the two on the CustomerID field and change the join from INNER JOIN to Left Join.

If you have to create a subquery for TableB as well, then this becomes a little more difficult.  If that's the case, I would generally write the SQL directly in the SQL View.  It might look something like:

SELECT A.CustomerID, A.Field2, A.Field3, B.FieldX, B.FieldY
FROM (SELECT CustomerID, Field2, Field3
           FROM TableA
           WHERE TableA.DateField BETWEEN #1/1/2010# AND #2/14/2010#) as A
LEFT JOIN
         (SELECT CustomerID, FieldX, FieldY
          FROM TableB
          WHERE TableB.SomeDateField BETWEEN #1/1/2010# AND #2/14/2010#) as B
ON A.CustomerID = B.CustomerID

This query would give you all the records from TableA where DateField is between 1 Jan 10 and 14 Feb 10,
and would join that to all the records from Table B where [SomeDateField] is in the same date range.  Given your earlier statement, This would result in some records which do not have matches in B for A.CustomerID, and would result in NULL values in FieldX and FieldY.
Random Solutions  
 
programming4us programming4us