Results 1 to 6 of 6
  1. #1
    kestefon is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Nov 2013
    Posts
    108

    Joining Tables Based on Date

    I have a number of tables with different metrics. However, they all have a "Date" field in common, broken out at the daily level. Since some of my tables have different start and end dates, I'd like missing values to simply appear as "N/A". Any idea how I would do this with SQL?



    I've attached a screenshot to give you an idea of the layout I'm going for. Each metric field (i.e. "Metric1", etc.) would be pulled in from a different table.

    Click image for larger version. 

Name:	DataExample.png 
Views:	10 
Size:	14.7 KB 
ID:	15247

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,926
    You need a master dataset of all possible date values. This dataset can be created with a UNION of the date fields from all the metrics tables.

    Then each metrics table can be joined to the master. Join type "Include all records from {master dataset name} and only those from {metric table name} that match"

    However, if a date is not in any of the metrics tables, there will be no record for that date in the final query.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    kestefon is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Nov 2013
    Posts
    108
    Thanks, that works!

    Quick syntax question: How do I left join to multiple tables? The code below correctly joins my "AllDates" query to the first dataset (UniqueVisitors), but I need to include fields from a number of other tables as well. For example, let's say I want to add Metric2 from Table2, and Metric3 from Table3.

    Code:
    SELECT dt.mDate, uv.DailyUniqueVisitors
    FROM AllDates AS dt LEFT JOIN UniqueVisitors as uv
    ON dt.mDate = uv.Date;

  4. #4
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,926
    Use the Access query builder to construct the SQL. Just add tables to the window and link on the key fields. Then if you want SQL in VBA procedure, copy/paste and make appropriate edits.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    kestefon is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Nov 2013
    Posts
    108
    Hi June7,

    Is it possible to do a UNION query using the Access query builder? I need to find all of the dates across about 20 tables...

  6. #6
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,926
    UNION must be typed or copy/paste in SQL View window of query builder. Limit of 50 SELECT lines.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

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

Similar Threads

  1. Joining two tables
    By rspai in forum Queries
    Replies: 7
    Last Post: 05-22-2012, 12:21 AM
  2. Joining 2 Tables based on a Common Relation to Another
    By StudentTeacher in forum Programming
    Replies: 5
    Last Post: 07-26-2011, 07:23 AM
  3. joining tables
    By frozendmj in forum Queries
    Replies: 5
    Last Post: 04-19-2011, 01:45 PM
  4. Joining more than 2 tables
    By anemoskkk in forum Access
    Replies: 6
    Last Post: 04-17-2011, 04:37 AM
  5. Help - Joining Tables..
    By fabian24 in forum Forms
    Replies: 3
    Last Post: 02-17-2011, 03:43 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