Results 1 to 7 of 7
  1. #1
    CodeLiftSleep is offline Advanced Beginner
    Windows 10 Access 2013 32bit
    Join Date
    May 2017
    Posts
    48

    Question Query with 2 LEFT JOINS to different tables not bringing back what I want

    I have three tables I am working with where I need to join together various data in a query.

    I have an accounts table, an Employee Averages table and a Client Table.

    I have a UDF called GetCurrUser() which I use in the query, which returns the user's EmployeeId via the Environ("username") variable. This is a defined query that runs on load to get the proper accounts for the person opening the report.

    In the accounts table, they are assigned accounts by the month, so for the employee I need to return ALL accounts where they have been assigned for ANY month. This part is working properly. Then I LEFT JOIN the Client table on accounts.ClientID = Client.ID and accounts.Grp# = client.Grp#. This part works fine as well.



    I then additionally LEFT JOIN EmpAverages.EmpId = accounts.EmpId...and this is where it doesn't always return the right information. Sometimes it returns the right employee data and other times it returns data for a different employee...I am assuming because I don't have anything in the WHERE clause with Emp.Name = GetCurrUser() ? I tried that and it didn't return anything...I also tried to do the LEFT JOIN on Emp.Name = GetCurrUser() and it threw an error saying I couldn't do that.

    Here is the full query below, I am hoping someone can help me out with getting it working properly and returning the information I need for that employee only.

    Code:
    SELECT t.SAM, a.ClientID, t.[Grp Name], IIf([t].[Tier Classification]="Tier 1","Tier 1","Tier 2") AS Tier, a.JanSAM, a.FebSAM, a.MarSAM, a.AprSAM, a.MaySAM, a.JunSAM, a.JulSAM, a.AugSAM, a.SepSAM, a.OctSAM, a.NovSAM, a.DecSAM, t.Segment, t.[GRP #], a.IsActive, a.JanActive, a.JanScore, a.FebActive, a.FebScore, a.MarActive, a.MarScore, a.AprActive, a.AprScore, a.MayActive, a.MayScore, a.JunActive, a.JunScore, a.JulActive, a.JulScore, a.AugActive, a.AugScore, a.SepActive, a.SepScore, a.OctActive, a.OctScore, a.NovActive, a.NovScore, a.DecActive, a.DecScore, a.YTDScore, EmpAverages.Tier1Avg, EmpAverages.Tier2Avg, EmpAverages.YTDAvg, a.EmpID, EmpAverages.EmpId
    FROM (Accounts AS a LEFT JOIN UnionT AS t ON (a.ClientID = t.ID) AND (a.[GRP #] = t.[GRP #])) LEFT JOIN EmpAverages.EmpId = a.EmpId
    WHERE (((a.JanSAM)=GetCurrUser())) OR (((a.FebSAM)=GetCurrUser())) OR (((a.MarSAM)=GetCurrUser())) OR (((a.AprSAM)=GetCurrUser())) OR (((a.MaySAM)=GetCurrUser())) OR (((a.JunSAM)=GetCurrUser())) OR (((a.JulSAM)=GetCurrUser())) OR (((a.AugSAM)=GetCurrUser())) OR (((a.SepSAM)=GetCurrUser())) OR (((a.OctSAM)=GetCurrUser())) OR (((a.NovSAM)=GetCurrUser())) OR (((a.DecSAM)=GetCurrUser()));

  2. #2
    aytee111 is offline Competent At Times
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    Keep things as simple as possible - always. Separate these into separate queries, you say the first part works, leave that as one query and then join in the averages info.

    Note: when the database opens call GetCurrUser and put it as a text box on the main form, then always refer to it rather than repeating the subroutine. Also, Access does not need the table alias's, generally it is not used. Optional however.

  3. #3
    CodeLiftSleep is offline Advanced Beginner
    Windows 10 Access 2013 32bit
    Join Date
    May 2017
    Posts
    48
    I'm not sure that would really help...the LEFT JOIN is still bringing back the wrong data at times...Perhaps rewrite it as a subquery?

    Why wouldn't I want to use a table alias instead of typing out the full name of the table?

  4. #4
    aytee111 is offline Competent At Times
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    First you are saying it wouldn't help, then you say it is still bringing back the wrong data. Did you try it?

    It is user choice, but the norm is not to use them, that is all I am saying.

    I am wondering about your table structure, with one record containing all the months/emp ID. It would be preferable to have those as separate records.

  5. #5
    CodeLiftSleep is offline Advanced Beginner
    Windows 10 Access 2013 32bit
    Join Date
    May 2017
    Posts
    48
    Quote Originally Posted by aytee111 View Post
    First you are saying it wouldn't help, then you say it is still bringing back the wrong data. Did you try it?

    It is user choice, but the norm is not to use them, that is all I am saying.

    I am wondering about your table structure, with one record containing all the months/emp ID. It would be preferable to have those as separate records.
    It probably would make sense to do that, but unfortunately I don't have control over that, I just have to get it working with as it is currently structured...

    So you are saying to create a new query, and pull the information in from the first query and then do a join on that data?

  6. #6
    aytee111 is offline Competent At Times
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    Yes. The first part is working properly. Now you will have a query with all the right info, with employees. Now make a new query and join in the last table. At the very least it will be easier to troubleshoot.

  7. #7
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,412
    cross posted here https://www.access-programmers.co.uk...d.php?t=298339

    OP advised of crossposting etiquette

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

Similar Threads

  1. Replies: 3
    Last Post: 11-13-2017, 03:09 PM
  2. Replies: 4
    Last Post: 08-09-2017, 12:06 PM
  3. Two left joins help
    By goalnightx in forum Queries
    Replies: 27
    Last Post: 03-01-2017, 07:10 PM
  4. Replies: 16
    Last Post: 06-06-2016, 08:45 AM
  5. Help With Left Joins
    By DaveyJ in forum Queries
    Replies: 23
    Last Post: 06-28-2010, 08:38 AM

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