Results 1 to 4 of 4
  1. #1
    skydivetom is offline VIP
    Windows 8 Access 2010 64bit
    Join Date
    Feb 2019
    Posts
    1,046

    UNION query

    Experts:



    I need some assistance with a UNION query. Allow me to provide some background first.

    In my database, I have three "types of entities": CFT, OPT, and WS

    To group a range of queries, I use the following prefixes for these types/entities, for example:
    1. "CFT" -- for this query set, queries are prefixed with a "Q202"
    2. "OPT" -- for this query set, queries are prefixed with a "Q302"
    2. "WS" -- for this query set, queries are prefixed with a "Q402"

    Now, the following three (3) queries **successfully execute** when I run them individually. SQL are as follows:

    For CFT:
    =====
    Code:
    SELECT 
    T01_StaffMembers.All_LastName AS LastName
    FROM (T01_StaffMembers LEFT JOIN Q202_CFT_Participation_NonAssignedStaff_Input 
    ON (T01_StaffMembers.All_RankTitle = Q202_CFT_Participation_NonAssignedStaff_Input.Rank_Grade) 
    AND (T01_StaffMembers.All_FirstName = Q202_CFT_Participation_NonAssignedStaff_Input.FirstName) 
    AND (T01_StaffMembers.All_LastName = Q202_CFT_Participation_NonAssignedStaff_Input.LastName)) 
    LEFT JOIN (T01_Organization RIGHT JOIN (T01_Billets RIGHT JOIN T00_JunctionTable_OBS 
    ON T01_Billets.BilletIDpk = T00_JunctionTable_OBS.BilletIDfk) 
    ON T01_Organization.OrganizationIDpk = T00_JunctionTable_OBS.OrganizationIDfk) 
    ON T01_StaffMembers.StaffMemberIDpk = T00_JunctionTable_OBS.StaffMemberIDfk
    WHERE (((T01_StaffMembers.All_LastName)<>"[Vacant]") 
    AND ((T01_StaffMembers.All_Onboard)=True) 
    AND ((Q202_CFT_Participation_NonAssignedStaff_Input.LastName) Is Null))
    ORDER BY T01_StaffMembers.All_LastName;
    For OPT:
    =====
    Code:
    SELECT T01_StaffMembers.All_LastName AS LastName
    FROM (T01_StaffMembers LEFT JOIN Q302_OPT_Participation_NonAssignedStaff_Input 
    ON (T01_StaffMembers.All_RankTitle = Q302_OPT_Participation_NonAssignedStaff_Input.Rank_Grade) 
    AND (T01_StaffMembers.All_FirstName = Q302_OPT_Participation_NonAssignedStaff_Input.FirstName) 
    AND (T01_StaffMembers.All_LastName = Q302_OPT_Participation_NonAssignedStaff_Input.LastName)) 
    LEFT JOIN (T01_Organization RIGHT JOIN (T01_Billets RIGHT JOIN T00_JunctionTable_OBS 
    ON T01_Billets.BilletIDpk = T00_JunctionTable_OBS.BilletIDfk) 
    ON T01_Organization.OrganizationIDpk = T00_JunctionTable_OBS.OrganizationIDfk) 
    ON T01_StaffMembers.StaffMemberIDpk = T00_JunctionTable_OBS.StaffMemberIDfk
    WHERE (((T01_StaffMembers.All_LastName)<>"[Vacant]") 
    AND ((T01_StaffMembers.All_Onboard)=True) 
    AND ((Q302_OPT_Participation_NonAssignedStaff_Input.LastName) Is Null))
    ORDER BY T01_StaffMembers.All_LastName;
    For WS:
    =====
    Code:
    SELECT T01_StaffMembers.All_LastName AS LastName
    FROM (T01_StaffMembers LEFT JOIN Q402_WS_Participation_NonAssignedStaff_Input 
    ON (T01_StaffMembers.All_RankTitle = Q402_WS_Participation_NonAssignedStaff_Input.Rank_Grade) 
    AND (T01_StaffMembers.All_FirstName = Q402_WS_Participation_NonAssignedStaff_Input.FirstName) 
    AND (T01_StaffMembers.All_LastName = Q402_WS_Participation_NonAssignedStaff_Input.LastName)) 
    LEFT JOIN (T01_Organization RIGHT JOIN (T01_Billets RIGHT JOIN T00_JunctionTable_OBS 
    ON T01_Billets.BilletIDpk = T00_JunctionTable_OBS.BilletIDfk) 
    ON T01_Organization.OrganizationIDpk = T00_JunctionTable_OBS.OrganizationIDfk) 
    ON T01_StaffMembers.StaffMemberIDpk = T00_JunctionTable_OBS.StaffMemberIDfk
    WHERE (((T01_StaffMembers.All_LastName)<>"[Vacant]") 
    AND ((T01_StaffMembers.All_Onboard)=True) 
    AND ((Q402_WS_Participation_NonAssignedStaff_Input.LastName) Is Null));
    ... so far so good!

    Now, while the query structures for CFT, OPT, and WS are identical (I am quite confident), I want to **UNION** all three queries into a single query. However, I'm getting an error (see attached JGP) when attempting to execute the UNION query.

    Please keep in mind, I can successfully execute the UNION query for "OPT" (type/entity #2) and "WS" (type/entity #3). It's only when I add "CPT" (type/entity #1) into the UNION query when it results in the error.

    Again, below SQL (OPT and WS) UNION query works fine:

    Code:
    SELECT T01_StaffMembers.All_LastName AS LastName
    FROM (T01_StaffMembers LEFT JOIN Q302_OPT_Participation_NonAssignedStaff_Input 
    ON (T01_StaffMembers.All_RankTitle = Q302_OPT_Participation_NonAssignedStaff_Input.Rank_Grade) 
    AND (T01_StaffMembers.All_FirstName = Q302_OPT_Participation_NonAssignedStaff_Input.FirstName) 
    AND (T01_StaffMembers.All_LastName = Q302_OPT_Participation_NonAssignedStaff_Input.LastName)) 
    LEFT JOIN (T01_Organization RIGHT JOIN (T01_Billets RIGHT JOIN T00_JunctionTable_OBS 
    ON T01_Billets.BilletIDpk = T00_JunctionTable_OBS.BilletIDfk) 
    ON T01_Organization.OrganizationIDpk = T00_JunctionTable_OBS.OrganizationIDfk) 
    ON T01_StaffMembers.StaffMemberIDpk = T00_JunctionTable_OBS.StaffMemberIDfk
    WHERE (((T01_StaffMembers.All_LastName)<>"[Vacant]") 
    AND ((T01_StaffMembers.All_Onboard)=True) 
    AND ((Q302_OPT_Participation_NonAssignedStaff_Input.LastName) Is Null))
    ORDER BY T01_StaffMembers.All_LastName;
    UNION 
    SELECT T01_StaffMembers.All_LastName AS LastName
    FROM (T01_StaffMembers LEFT JOIN Q402_WS_Participation_NonAssignedStaff_Input 
    ON (T01_StaffMembers.All_RankTitle = Q402_WS_Participation_NonAssignedStaff_Input.Rank_Grade) 
    AND (T01_StaffMembers.All_FirstName = Q402_WS_Participation_NonAssignedStaff_Input.FirstName) 
    AND (T01_StaffMembers.All_LastName = Q402_WS_Participation_NonAssignedStaff_Input.LastName)) 
    LEFT JOIN (T01_Organization RIGHT JOIN (T01_Billets RIGHT JOIN T00_JunctionTable_OBS 
    ON T01_Billets.BilletIDpk = T00_JunctionTable_OBS.BilletIDfk) 
    ON T01_Organization.OrganizationIDpk = T00_JunctionTable_OBS.OrganizationIDfk) 
    ON T01_StaffMembers.StaffMemberIDpk = T00_JunctionTable_OBS.StaffMemberIDfk
    WHERE (((T01_StaffMembers.All_LastName)<>"[Vacant]") 
    AND ((T01_StaffMembers.All_Onboard)=True) 
    AND ((Q402_WS_Participation_NonAssignedStaff_Input.LastName) Is Null));
    However, the SQL below (CFT and OPT and WS) results in the error on the [All_LastName]. What am I missing/overlooking? How can I fix the code so that I can UNION all three individual queries?

    Code:
    SELECT 
    T01_StaffMembers.All_LastName AS LastName
    FROM (T01_StaffMembers LEFT JOIN Q202_CFT_Participation_NonAssignedStaff_Input 
    ON (T01_StaffMembers.All_RankTitle = Q202_CFT_Participation_NonAssignedStaff_Input.Rank_Grade) 
    AND (T01_StaffMembers.All_FirstName = Q202_CFT_Participation_NonAssignedStaff_Input.FirstName) 
    AND (T01_StaffMembers.All_LastName = Q202_CFT_Participation_NonAssignedStaff_Input.LastName)) 
    LEFT JOIN (T01_Organization RIGHT JOIN (T01_Billets RIGHT JOIN T00_JunctionTable_OBS 
    ON T01_Billets.BilletIDpk = T00_JunctionTable_OBS.BilletIDfk) 
    ON T01_Organization.OrganizationIDpk = T00_JunctionTable_OBS.OrganizationIDfk) 
    ON T01_StaffMembers.StaffMemberIDpk = T00_JunctionTable_OBS.StaffMemberIDfk
    WHERE (((T01_StaffMembers.All_LastName)<>"[Vacant]") 
    AND ((T01_StaffMembers.All_Onboard)=True) 
    AND ((Q202_CFT_Participation_NonAssignedStaff_Input.LastName) Is Null))
    ORDER BY T01_StaffMembers.All_LastName;
    UNION 
    SELECT T01_StaffMembers.All_LastName AS LastName
    FROM (T01_StaffMembers LEFT JOIN Q302_OPT_Participation_NonAssignedStaff_Input 
    ON (T01_StaffMembers.All_RankTitle = Q302_OPT_Participation_NonAssignedStaff_Input.Rank_Grade) 
    AND (T01_StaffMembers.All_FirstName = Q302_OPT_Participation_NonAssignedStaff_Input.FirstName) 
    AND (T01_StaffMembers.All_LastName = Q302_OPT_Participation_NonAssignedStaff_Input.LastName)) 
    LEFT JOIN (T01_Organization RIGHT JOIN (T01_Billets RIGHT JOIN T00_JunctionTable_OBS 
    ON T01_Billets.BilletIDpk = T00_JunctionTable_OBS.BilletIDfk) 
    ON T01_Organization.OrganizationIDpk = T00_JunctionTable_OBS.OrganizationIDfk) 
    ON T01_StaffMembers.StaffMemberIDpk = T00_JunctionTable_OBS.StaffMemberIDfk
    WHERE (((T01_StaffMembers.All_LastName)<>"[Vacant]") 
    AND ((T01_StaffMembers.All_Onboard)=True) 
    AND ((Q302_OPT_Participation_NonAssignedStaff_Input.LastName) Is Null))
    ORDER BY T01_StaffMembers.All_LastName;
    UNION 
    SELECT T01_StaffMembers.All_LastName AS LastName
    FROM (T01_StaffMembers LEFT JOIN Q402_WS_Participation_NonAssignedStaff_Input 
    ON (T01_StaffMembers.All_RankTitle = Q402_WS_Participation_NonAssignedStaff_Input.Rank_Grade) 
    AND (T01_StaffMembers.All_FirstName = Q402_WS_Participation_NonAssignedStaff_Input.FirstName) 
    AND (T01_StaffMembers.All_LastName = Q402_WS_Participation_NonAssignedStaff_Input.LastName)) 
    LEFT JOIN (T01_Organization RIGHT JOIN (T01_Billets RIGHT JOIN T00_JunctionTable_OBS 
    ON T01_Billets.BilletIDpk = T00_JunctionTable_OBS.BilletIDfk) 
    ON T01_Organization.OrganizationIDpk = T00_JunctionTable_OBS.OrganizationIDfk) 
    ON T01_StaffMembers.StaffMemberIDpk = T00_JunctionTable_OBS.StaffMemberIDfk
    WHERE (((T01_StaffMembers.All_LastName)<>"[Vacant]") 
    AND ((T01_StaffMembers.All_Onboard)=True) 
    AND ((Q402_WS_Participation_NonAssignedStaff_Input.LastName) Is Null));
    Attached Thumbnails Attached Thumbnails Error.JPG  

  2. #2
    JoeM is offline VIP
    Windows 7 32bit Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    It looks like that field appears twice in the "ORDER BY" clause of two of the Sub Queries.
    Try getting rid of the "ORDER BY" line in those Sub Queries, it isn't necessary.
    There really isn't any point in having those lines there in UNION queries, since you are combining them all together.

  3. #3
    skydivetom is offline VIP
    Windows 8 Access 2010 64bit
    Join Date
    Feb 2019
    Posts
    1,046
    JoeM:

    Wow... a simple yet perfect solution! I would have never thought that the order would cause the issue.
    UNION query works great now. Thousand thanks!



    EEH

  4. #4
    JoeM is offline VIP
    Windows 7 32bit Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    You are welcome!

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

Similar Threads

  1. Replies: 3
    Last Post: 01-03-2020, 12:36 PM
  2. Replies: 3
    Last Post: 11-29-2018, 03:18 PM
  3. Replies: 4
    Last Post: 08-09-2017, 12:06 PM
  4. Convert Union Query to non-union help
    By Ekhart in forum Queries
    Replies: 2
    Last Post: 01-10-2017, 03:39 AM
  5. Replies: 4
    Last Post: 12-20-2015, 02:35 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