Page 2 of 3 FirstFirst 123 LastLast
Results 16 to 30 of 31
  1. #16
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,932
    I repeat my previous answer



    1. you seem to be missing a bracket before the group by
    2. you have left in the bits in green - they should be removed

  2. #17
    Bulzie is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    Nov 2015
    Posts
    1,511
    Try what I suggested, simple 2 queries (if it works ). Don't always have to use VBA, Access query builder is powerful, why not use it. If it works look at the SQL code to learn syntax.

  3. #18
    melvinhagar is offline Novice
    Windows 7 32bit Access 2013 32bit
    Join Date
    Aug 2016
    Posts
    23
    Executed as indicated,.... syntax errors returned again with those changes. The following SQL will run (no errors), but just not returning desired results.


    SELECT [SWP Employee Historical Database - Terminations].[Workday Empl ID], [SWP Employee Historical Database - Terminations].[Term Date], [SWP Employee Historical Database - Job & Organization Data].[Record Effective Date], [SWP Employee Historical Database - Job & Organization Data].[Empl Name]
    FROM [SWP Employee Historical Database - Terminations] INNER JOIN [SWP Employee Historical Database - Job & Organization Data] ON [SWP Employee Historical Database - Terminations].[Workday Empl ID] = [SWP Employee Historical Database - Job & Organization Data].[Workday Empl ID]
    WHERE ((([SWP Employee Historical Database - Job & Organization Data].[Record Effective Date])=(SELECT Max([SWP Employee Historical Database - Job & Organization Data].[Record Effective Date]) FROM [SWP Employee Historical Database - Job & Organization Data] T Where [SWP Employee Historical Database - Job & Organization Data].[Workday Empl ID]=[SWP Employee Historical Database - Terminations].[Workday Empl ID] And [SWP Employee Historical Database - Job & Organization Data].[Record Effective Date]<= [SWP Employee Historical Database - Terminations].[Term Date]
    GROUP BY [SWP Employee Historical Database - Terminations].[Workday Empl ID], [SWP Employee Historical Database - Terminations].[Term Date], [SWP Employee Historical Database - Job & Organization Data].[Record Effective Date], [SWP Employee Historical Database - Job & Organization Data].[Empl Name])))
    GROUP BY [SWP Employee Historical Database - Terminations].[Workday Empl ID], [SWP Employee Historical Database - Terminations].[Term Date], [SWP Employee Historical Database - Job & Organization Data].[Record Effective Date], [SWP Employee Historical Database - Job & Organization Data].[Empl Name]
    HAVING ((([SWP Employee Historical Database - Terminations].[Workday Empl ID])='62111566'));

  4. #19
    melvinhagar is offline Novice
    Windows 7 32bit Access 2013 32bit
    Join Date
    Aug 2016
    Posts
    23
    To Bulzie: not sure what you mean with Query2 when you say, "Use Query1,....". This surely cannot be this diffiult. If it is, then I'm not too impressed with Access Query. My current version of the SQL is below, which will execute. It just returns the multiple rows because of table 2 (Job & Organization Table).

    SELECT [SWP Employee Historical Database - Terminations].[Workday Empl ID], [SWP Employee Historical Database - Terminations].[Term Date], [SWP Employee Historical Database - Job & Organization Data].[Record Effective Date], [SWP Employee Historical Database - Job & Organization Data].[Empl Name]
    FROM [SWP Employee Historical Database - Terminations] INNER JOIN [SWP Employee Historical Database - Job & Organization Data] ON [SWP Employee Historical Database - Terminations].[Workday Empl ID] = [SWP Employee Historical Database - Job & Organization Data].[Workday Empl ID]
    WHERE ((([SWP Employee Historical Database - Job & Organization Data].[Record Effective Date])=(SELECT Max([SWP Employee Historical Database - Job & Organization Data].[Record Effective Date]) FROM [SWP Employee Historical Database - Job & Organization Data] T Where [SWP Employee Historical Database - Job & Organization Data].[Workday Empl ID]=[SWP Employee Historical Database - Terminations].[Workday Empl ID] And [SWP Employee Historical Database - Job & Organization Data].[Record Effective Date]<= [SWP Employee Historical Database - Terminations].[Term Date]
    GROUP BY [SWP Employee Historical Database - Terminations].[Workday Empl ID], [SWP Employee Historical Database - Terminations].[Term Date], [SWP Employee Historical Database - Job & Organization Data].[Record Effective Date], [SWP Employee Historical Database - Job & Organization Data].[Empl Name])))
    GROUP BY [SWP Employee Historical Database - Terminations].[Workday Empl ID], [SWP Employee Historical Database - Terminations].[Term Date], [SWP Employee Historical Database - Job & Organization Data].[Record Effective Date], [SWP Employee Historical Database - Job & Organization Data].[Empl Name]
    HAVING ((([SWP Employee Historical Database - Terminations].[Workday Empl ID])='62111566'));
    Last edited by melvinhagar; 08-08-2016 at 12:08 PM. Reason: clarification

  5. #20
    Bulzie is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    Nov 2015
    Posts
    1,511
    Create Query1 and Save it. Run it to make sure it brings back the records like your example, all records <=Date.
    Create Query2, Select Query1 as the "table" inside and do what I said for this second query. Run it and see if it works and gives you what you are requesting.

  6. #21
    melvinhagar is offline Novice
    Windows 7 32bit Access 2013 32bit
    Join Date
    Aug 2016
    Posts
    23
    To Bulzie: Yep, that worked for my one test employee. It's still desirable to be able to do it within one query, but at least I may have a workaround. Thanks guy!!!

  7. #22
    melvinhagar is offline Novice
    Windows 7 32bit Access 2013 32bit
    Join Date
    Aug 2016
    Posts
    23
    To Bulzie: I guess I spoke to soon. The solution worked for my one "test" employee, now it's not working for the entire set of data.

  8. #23
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,932
    what happened to the code I provided in post #8 - restated here from your code with corrections

    Code:
    SELECT [SWP Employee Historical Database - Terminations].[Workday Empl ID], [SWP Employee Historical Database - Terminations].[Term Date], [SWP Employee Historical Database - Job & Organization Data].[Record Effective Date], [SWP Employee Historical Database - Job & Organization Data].[Empl Name]
     FROM [SWP Employee Historical Database - Terminations] INNER JOIN [SWP Employee Historical Database - Job & Organization Data] ON [SWP Employee Historical Database - Terminations].[Workday Empl ID] = [SWP Employee Historical Database - Job & Organization Data].[Workday Empl ID]
     WHERE [SWP Employee Historical Database - Job & Organization Data].[Record Effective Date]=(SELECT Max([Record Effective Date]) FROM [SWP Employee Historical Database - Job & Organization Data] T Where [Workday Empl ID]=[SWP Employee Historical Database - Terminations].[Workday Empl ID] And [Record Effective Date]<= [SWP Employee Historical Database - Terminations].[Term Date])
    is all you should need

    or using aliasing to make it more readable

    Code:
    SELECT E.[Workday Empl ID], E.[Term Date], O.[Record Effective Date], O.[Empl Name]
     FROM [SWP Employee Historical Database - Terminations] E INNER JOIN [SWP Employee Historical Database - Job & Organization Data] O ON E.[Workday Empl ID] = O.[Workday Empl ID]
     WHERE O.[Record Effective Date]=(SELECT Max([Record Effective Date]) FROM [SWP Employee Historical Database - Job & Organization Data] T Where [Workday Empl ID]=E.[Workday Empl ID] And [Record Effective Date]<=E.[Term Date])
    Last edited by CJ_London; 08-08-2016 at 03:49 PM.

  9. #24
    melvinhagar is offline Novice
    Windows 7 32bit Access 2013 32bit
    Join Date
    Aug 2016
    Posts
    23
    To Bulzie: I made a minor change to set "Max" on the record effective date and the employee ID and it seemed to work now.

  10. #25
    melvinhagar is offline Novice
    Windows 7 32bit Access 2013 32bit
    Join Date
    Aug 2016
    Posts
    23
    To Bulzie: I spoke to soon.. still getting mult rows fromt the 2nd table... it's not returning the Max row...

  11. #26
    melvinhagar is offline Novice
    Windows 7 32bit Access 2013 32bit
    Join Date
    Aug 2016
    Posts
    23
    To Ajax: The corrected SQL you supplied is running,... but it's been nearly 30 minutes and results aren't back..

  12. #27
    Bulzie is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    Nov 2015
    Posts
    1,511
    Are you talking about Query1 results or Query2 are returning multiple records. Query1 does return multiple records for same ID. Query2 is where you group it to get 1 record. Make sure in Query2 the Date field you have Max on is he first field in Query2.

  13. #28
    melvinhagar is offline Novice
    Windows 7 32bit Access 2013 32bit
    Join Date
    Aug 2016
    Posts
    23
    It was Query2 still returning mult rows... I'll recheck it. Currently, the SQL supplied by Ajax is still running.... anxious to see what it will return-- if it ever finishes.

  14. #29
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,932
    To Ajax: The corrected SQL you supplied is running,... but it's been nearly 30 minutes and results aren't back..
    you need to have indexed the appropriate fields for a good performance. In this case

    [Workday Empl ID] in both tables
    and [SWP Employee Historical Database - Job & Organization Data].[Record Effective Date]

  15. #30
    melvinhagar is offline Novice
    Windows 7 32bit Access 2013 32bit
    Join Date
    Aug 2016
    Posts
    23
    Thanks. I will confirm indexes on my tables if/when the query ever finishes. about 45 minutes now. Table 1 has 5,000 rows,... Table 2 has 900,000 rows....

Page 2 of 3 FirstFirst 123 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Joining multiple tables get wrong result
    By Tony Thi in forum Queries
    Replies: 3
    Last Post: 10-16-2015, 08:54 AM
  2. Multiple criteria query joining two tables
    By Carnior in forum Queries
    Replies: 15
    Last Post: 05-01-2015, 08:24 AM
  3. Replies: 2
    Last Post: 10-12-2013, 11:17 AM
  4. Joining 2 tables on multiple fields
    By smoothlarryhughes in forum Queries
    Replies: 6
    Last Post: 12-13-2012, 10:40 AM
  5. Joining Unioins output to tables? Help Needed
    By techexpressinc in forum Queries
    Replies: 4
    Last Post: 06-24-2009, 05:05 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