Results 1 to 9 of 9
  1. #1
    Fuii is offline Novice
    Windows 10 Office 365
    Join Date
    Aug 2020
    Posts
    12

    Mapping data via join and Nz() for dates

    Hi folks,



    I've got an issue I did solve couple of years ago already, but I can't remember how - the curse of doing it only once.

    I've got 2 tables.

    Table 1 - history - contains an integer and a date field. Duplicates allowed.
    Table 2 - Map - contains an integer, 2 date fields and an additional field that is to be added in the query. Duplicates allowed.

    Both tables are joined via the integer fields:

    Click image for larger version. 

Name:	Query.JPG 
Views:	25 
Size:	26.6 KB 
ID:	42691


    The intention is to get all lines from "history" and add the "DataToAdd" column from "Map" to it, though both tables contain duplicates for the join clause.
    To get the correct one, the History.DateClosed must be inbetween of Map.DateStart and Map.DateEnd.
    Additionally, Map.DateEnd can be empty/null - I guess using Nz() here with Now() as null clause.

    My issue is, that I can't figure out the proper filtering in combination with Nz().

    Here are some samples and the intended output:

    Click image for larger version. 

Name:	Tables.jpg 
Views:	25 
Size:	72.1 KB 
ID:	42692


    Thanks in advance.

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    I assume what you're hung up on is the join. In addition to the join on job/employee, you want a non-equi join on the date fields. That can't be represented in the design grid, so you'll have to do it in SQL view. It would look like:

    ...And DateField >= FromDate And DateField <= ToDate

    I'm on a mobile so hard to type but hopefully that points you in the right direction.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    Fuii is offline Novice
    Windows 10 Office 365
    Join Date
    Aug 2020
    Posts
    12
    Thanks for your reply. I did extend the Join via SQL as suggested, but the query returned no results.

    Query:

    Code:
    SELECT History.JobID, History.DateClosed, Map.DataToAdd
    FROM History 
    INNER JOIN Map ON History.JobID = Map.EmpID 
        and History.DateClosed >= Map.DateStart
        and History.DateClosed <= Map.DateEnd

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    You haven't included the Nz(). I don't know if it will work in the join, you may need to use a base query that applies it. If that doesn't help, can you attach a sample of the db here?
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,424
    have you tried
    Code:
    SELECT History.JobID, History.DateClosed, Map.DataToAdd
    FROM History INNER JOIN Map ON History.JobID = Map.EmpID
    WHERE History.DateClosed >= Map.DateStart     and History.DateClosed <= nz(Map.DateEnd,History.DateClosed)
    you mention using the now function - that returns date and time. To return just the date, use the date function

  6. #6
    Fuii is offline Novice
    Windows 10 Office 365
    Join Date
    Aug 2020
    Posts
    12
    Thanks for our assistance so far guys.

    Using Ajax' query or the adjusted from pbaldy with included Nz() yielding the same result:

    Click image for larger version. 

Name:	QueryResult.JPG 
Views:	15 
Size:	17.5 KB 
ID:	42747

    The rows with ID 102 that have an end date of 07/31/2020 are missing. :/

    Attached is the sample DB including your both queries.


    you mention using the now function - that returns date and time. To return just the date, use the date function

    Due to working with Date/Time fields in queries and extract dates or times from them, I'm used to do Int(Now()) in that case - which gets the current date too - old habit.
    Attached Files Attached Files

  7. #7
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    I think my query works (probably Ajax's too), the problem is the DateEnd value of 7/31/202. Note 202, not 2020.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  8. #8
    Fuii is offline Novice
    Windows 10 Office 365
    Join Date
    Aug 2020
    Posts
    12
    hmm, *blush*
    I think I'll just sink into the ground, in a dark corner.

    Indeed, after correcting the year both queries work as intended.

    Click image for larger version. 

Name:	Thanks!.JPG 
Views:	12 
Size:	73.3 KB 
ID:	42768

  9. #9
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    LOL! No problem, we've all done it.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 8
    Last Post: 03-15-2018, 12:23 PM
  2. Replies: 2
    Last Post: 02-11-2014, 09:38 AM
  3. Join Tables with differing Dates
    By sberti in forum Queries
    Replies: 2
    Last Post: 11-29-2012, 10:22 PM
  4. Replies: 10
    Last Post: 11-16-2011, 10:58 AM
  5. Importing and mapping data to various tables
    By rasticle in forum Import/Export Data
    Replies: 1
    Last Post: 05-17-2010, 04:22 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums