Results 1 to 7 of 7
  1. #1
    bethanysmith is offline Novice
    Windows 2K Access 2010 64bit
    Join Date
    Mar 2015
    Posts
    2

    Reverse Engineer This Query - Design View & SQL

    Hello, I have an Access database that I've inherited that I am now tasked with replacing. I'm supposed to determine what this database is doing and replace it - I'm stuck on a particular query that just doesn't seem to make sense to me. I've attached both the query view and the design view. It's possible that the person who built this had a flaw in their logic, but it's also possible that I just don't get it. Any insight would be appreciated!



    Click image for larger version. 

Name:	Capture.PNG 
Views:	12 
Size:	33.0 KB 
ID:	19920

    SQL View (line and paragraph breaks added for ease of reading):

    SELECT
    tblsupvtoemp.POSN,
    IIf(IsNull([PWD_EXECRPT_PAF_2].[EMPLOYEE_ID]),[PWD_EXECRPT_PAF_1].[EMPLOYEE_ID],[PWD_EXECRPT_PAF_2].[EMPLOYEE_ID]) AS Supervisor_Employee_ID,
    IIf(IsNull([PWD_EXECRPT_PAF_2].[LAST_NAME]),[PWD_EXECRPT_PAF_1].[LAST_NAME],[PWD_EXECRPT_PAF_2].[LAST_NAME]) AS Supervisor_Last_Name,
    IIf(IsNull([PWD_EXECRPT_PAF_2].[First_NAME]),[PWD_EXECRPT_PAF_1].[First_NAME],[PWD_EXECRPT_PAF_2].[First_NAME]) AS Supervisor_First_Name

    FROM
    ((tblsupvtoemp LEFT JOIN tblsupvtoemp AS tblsupvtoemp_1 ON tblsupvtoemp.[POSN reported to] = tblsupvtoemp_1.POSN) LEFT JOIN PWD_EXECRPT_PAF AS PWD_EXECRPT_PAF_1 ON tblsupvtoemp_1.[POSN reported to] = PWD_EXECRPT_PAF_1.POSN)

    LEFT JOIN PWD_EXECRPT_PAF AS PWD_EXECRPT_PAF_2 ON tblsupvtoemp.[POSN reported to] = PWD_EXECRPT_PAF_2.POSN

    WHERE (((tblsupvtoemp.[POSN reported to]) Is Not Null) AND ((tblsupvtoemp_1.[POSN reported to]) Is Not Null) AND ((PWD_EXECRPT_PAF_2.EMPLOYEE_ID) Is Null));

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    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
    bethanysmith is offline Novice
    Windows 2K Access 2010 64bit
    Join Date
    Mar 2015
    Posts
    2
    Thank you for your response. I understand the concept of self-joins, but it is some of the deeper logic I don't understand.

    I feel like the query is getting the employee information, the supervisor of that employeee's information, and then the next supervisor up as well. At least the joins seem to be going up that next level. But there are some criteria that I don't follow the logic of - the is null and is not null - and then why the query is pulling what seems to be the same supervisor's first and last name with the way the IIF statements are being used.

    If anyone is game to totally reverse engineer this query with me, I'm appreciative!

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    Remove the criteria and see what the dataset looks like.
    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
    Rawb is offline Expert
    Windows 7 64bit Access 2010 32bit
    Join Date
    Dec 2009
    Location
    Somewhere
    Posts
    875
    From what I can see, this query returns:
    The position of an employee and who they report to.
    The position of that employee (the one the first employee reports to) and who THEY report too.

    Employee -> Employee's Supervisor -> Supervisor's Supervisor

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    And what IIF statements?
    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.

  7. #7
    Rawb is offline Expert
    Windows 7 64bit Access 2010 32bit
    Join Date
    Dec 2009
    Location
    Somewhere
    Posts
    875
    It looks like those IIf statements just tell the Query that if the Supervisor's Supervisor can't be found, just use the Employee's Supervisor.

    For example: I'm listed as bethanysmith's Supervisor, June7 is listed as my Supervisor, and nobody is listed as June7's supervisor (June7 is The BossTM).

    You'd see the following
    Employee
    Supervisor
    Supervisor's Supervisor
    bethanysmith Rawb June7
    Rawb June7 June7
    Last edited by Rawb; 03-06-2015 at 09:09 AM. Reason: Clarification

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

Similar Threads

  1. Replies: 3
    Last Post: 12-02-2012, 12:59 AM
  2. Query - Design View
    By Rick West in forum Queries
    Replies: 4
    Last Post: 04-09-2012, 04:09 PM
  3. Unable to view query in design view
    By vemi007 in forum Queries
    Replies: 7
    Last Post: 01-19-2012, 11:36 AM
  4. How to "reverse engineer" a report
    By Buakaw in forum Reports
    Replies: 7
    Last Post: 01-18-2011, 06:20 AM
  5. Using a Design View Query in ADO?
    By danny2000 in forum Access
    Replies: 4
    Last Post: 12-06-2010, 03:36 AM

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