Results 1 to 5 of 5
  1. #1
    BCJourney is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Sep 2012
    Location
    Cincinnati, OH
    Posts
    46

    Query for same fields, different dates

    I have a query that is based on three queries. 2 of those queries have the same fields but return data for different dates. The 3rd query returns the dates needed. In other words, I am trying to return all results from both queries. these are the results I want:



    Report_ID Report_Date Contents Approvers
    Standard ops 5/1/13 daily operations rpt tom tomlin
    royer manuf 7/1/13 syndication rpt rob davis

    etc.

    These are for year-t0-date. there is a YTD query that returns all dates needed. The other two queries return records on different dates, on is for dates <5/5/13 and the other is for dates >5/4/13. I have listed the SQL below. It returns the correct records, but for the Record_ID it returns -1 in each field instead of the report_ID text. So, I need to get the other fields to show up correctly.

    Code:
    SELECT [Daily_rpts_YTD_due_before_5_5_2013]![Report_ID] Or [Daily_rpts_YTD_due_after_5_4_2013]![Report_ID] AS [Report ID], due_dates_Daily_YTD.Due_dates
    FROM (due_dates_Daily_YTD LEFT JOIN Daily_rpts_YTD_due_before_5_5_2013 ON due_dates_Daily_YTD.Due_dates = Daily_rpts_YTD_due_before_5_5_2013.Due_dates) LEFT JOIN Daily_rpts_YTD_due_after_5_4_2013 ON due_dates_Daily_YTD.Due_dates = Daily_rpts_YTD_due_after_5_4_2013.Due_dates
    WHERE (((due_dates_Daily_YTD.Due_dates)=[Daily_rpts_YTD_due_after_5_4_2013]![Due_dates] Or (due_dates_Daily_YTD.Due_dates)=[Daily_rpts_YTD_due_before_5_5_2013]![Due_dates]));

  2. #2
    Dal Jeanis is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2013
    Location
    Dallas TX
    Posts
    1,742

    Use NZ to detect the Null record

    I believe that's because it's evaluating this OR condition
    Code:
    [Daily_rpts_YTD_due_before_5_5_2013]![Report_ID] Or [Daily_rpts_YTD_due_after_5_4_2013]![Report_ID]
    to "Null" OR "True", rather than giving you one or the other.

    You need to replace that with an IIF/NZ structure.

    By the way, I highly recommend Aliasing your tables and queries. In design view, you can right-click the source table or source query and give it a short new name to make the SQL more readable. Your desired SQL should end up looking somewhat like this (I coded it by hand, so don't expect it to be perfect):
    Code:
    SELECT (NZ(DD5.[Report_ID], DD4.[Report_ID]) AS [Report ID], 
           DDY.Due_dates
    FROM 
       (
         (due_dates_Daily_YTD AS DDY
          LEFT JOIN 
          Daily_rpts_YTD_due_before_5_5_2013 AS DD5
          ON DDY.Due_dates = DD5.Due_dates
         ) 
       LEFT JOIN 
       Daily_rpts_YTD_due_after_5_4_2013 AS DD4 
       ON DDY.Due_dates = DD4.Due_dates
       )  
    WHERE 
       (
        ((DDY.Due_dates)=DD4.[Due_dates]) 
     OR ((DDY.Due_dates)=DD5.[Due_dates])
       );

  3. #3
    BCJourney is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Sep 2012
    Location
    Cincinnati, OH
    Posts
    46
    Thank you for your help. I ended up doing a UNION ALL query and it works!

  4. #4
    Dal Jeanis is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2013
    Location
    Dallas TX
    Posts
    1,742
    Good enough. If you don't mind, please post the version that worked, and mark the thread solved. Top of page, under thread tools.

  5. #5
    BCJourney is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Sep 2012
    Location
    Cincinnati, OH
    Posts
    46
    This is the UNION ALL query I did that worked:

    SELECT Report_ID,due_dates FROM Daily_rpts_YTD_due_before_5_5_2013
    UNION ALL
    SELECT Report_ID,due_dates
    FROM Daily_rpts_YTD_due_after_5_4_2013;

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

Similar Threads

  1. Replies: 1
    Last Post: 02-16-2013, 09:11 AM
  2. Replies: 3
    Last Post: 07-10-2012, 05:50 PM
  3. more trouble with text fields as dates
    By mitchmcc in forum Queries
    Replies: 3
    Last Post: 03-14-2012, 08:51 AM
  4. Query input dates for range of dates in two fields
    By kagoodwin13 in forum Queries
    Replies: 3
    Last Post: 02-13-2012, 04:25 PM
  5. Dates & Days Fields
    By djclntn in forum Database Design
    Replies: 5
    Last Post: 10-22-2011, 06:22 PM

Tags for this Thread

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