Search code examples
ms-accessms-access-2013

Making a query with iif function using between for dates to provide X


I am using MS access to create a query to determine if a birth date falls between a start date and an end date found in another table and if so provide a symbol found on the same table as the start and end dates.

I keep getting 2 results for each date of birth and don't know why.
Can anyone help out to only have one set of results per date of birth?
I have attached the code below.

SELECT [TBL_IndicativeData_AllPpts].ID, [TBL_IndicativeData_AllPpts]![DOB] AS DOB, IIf([TBL_IndicativeData_AllPpts]![DOB] Between [TBL_Mapping_Funds_TargetDate]![Earliest Birth Date] And [TBL_Mapping_Funds_TargetDate]![Latest Birth Date],[TBL_Mapping_Funds_TargetDate]![Fund Symbol],"Null") AS TargetDateFund

FROM TBL_Mapping_Funds_TargetDate, [TBL_IndicativeData_AllPpts]
WHERE (((IIf([TBL_IndicativeData_AllPpts]![DOB] Between [TBL_Mapping_Funds_TargetDate]![Earliest Birth Date] And [TBL_Mapping_Funds_TargetDate]![Latest Birth Date],[TBL_Mapping_Funds_TargetDate]![Fund Symbol],"Null"))<>"null"));

Solution

  • This will return the records that match the criteria:

    SELECT 
        [TBL_IndicativeData_AllPpts].ID, 
        [TBL_IndicativeData_AllPpts]![DOB],
        [TBL_Mapping_Funds_TargetDate]![Fund Symbol]
    FROM 
        [TBL_IndicativeData_AllPpts],
        [TBL_Mapping_Funds_TargetDate]
    WHERE 
        [TBL_IndicativeData_AllPpts]![DOB] Between 
            [TBL_Mapping_Funds_TargetDate]![Earliest Birth Date] And 
            [TBL_Mapping_Funds_TargetDate]![Latest Birth Date]
    

    However, it the TargetDate table has overlappeing date ranges, you may receive dupes. If so, try with SELECT DISTINCT ....