Results 1 to 5 of 5
  1. #1
    taryn is offline Novice
    Windows 10 Access 2016
    Join Date
    May 2019
    Posts
    17

    Combining differenct SELECT statements into one query

    Hello

    I do not know how to combine different SELECT statements into one coherent query. Essentially, I am trying to find active links to entities for a particular file code ‘BROW16’.

    These are my statements and the results that they yield separately:

    Statement 1:
    SELECT DISTINCT R.FileCode FROM ReportAccounts R JOIN RefMaster M ON R.FileCode=M.RefCode WHERE R.AcctHolder='BROW16' AND R.AcctStatus='A' AND M.Status='0'
    yields no results

    Statement 2:


    SELECT DISTINCT O.EntCode FROM Officers O JOIN RefMaster M ON O.EntCode=M.RefCode WHERE O.AddrCode='BROW16' AND O.Status='A' AND M.Status='0'
    yields three results:
    006850
    007193
    TESTLL

    Statement 3:
    SELECT I.RefCode FROM RefMaster I WHERE I.Cl_IntroBy='BROW16' AND I.Status='0'
    yields no results

    Statement 4:
    SELECT CL.EntCode FROM Entity CL WHERE CL.EntClient='BROW16' AND CL.Status IN ('0', '1')
    yields one result:
    ADTECH

    Statement 5:
    SELECT CO.EntCode FROM Entity CO WHERE CO.ContactAddrCode='BROW16' AND CO.Status IN ('0', '1')
    yields three results:
    006850
    008034
    ADTECH

    What I would like to do is be able to combine all 5 statements into one, eliminating any null and duplicated results and, if possible, having them appear in one row separated by a semi-colon ( and a space.

    In summary, I need a statement that will, ideally, return:
    006850; 007193; TESTLL; ADTECH; 008034

    The order of the codes does not matter.

    Is this possible and how would I go about it?

    Thank you for your assistance.

    Taryn

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,518
    Combining them is easy:

    SELECT DISTINCT R.FileCode FROM ReportAccounts R JOIN RefMaster M ON R.FileCode=M.RefCode WHERE R.AcctHolder='BROW16' AND R.AcctStatus='A' AND M.Status='0'
    UNION
    SELECT DISTINCT O.EntCode FROM Officers O JOIN RefMaster M ON O.EntCode=M.RefCode WHERE O.AddrCode='BROW16' AND O.Status='A' AND M.Status='0'
    UNION
    ...

    That should also eliminate duplicates. You'll need to loop the result in VBA to get them on one row separated by semicolons.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,825
    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.

  4. #4
    taryn is offline Novice
    Windows 10 Access 2016
    Join Date
    May 2019
    Posts
    17
    Thanks for your help. That works fine.

    Kind regards.

    Taryn

    Quote Originally Posted by pbaldy View Post
    Combining them is easy:

    SELECT DISTINCT R.FileCode FROM ReportAccounts R JOIN RefMaster M ON R.FileCode=M.RefCode WHERE R.AcctHolder='BROW16' AND R.AcctStatus='A' AND M.Status='0'
    UNION
    SELECT DISTINCT O.EntCode FROM Officers O JOIN RefMaster M ON O.EntCode=M.RefCode WHERE O.AddrCode='BROW16' AND O.Status='A' AND M.Status='0'
    UNION
    ...

    That should also eliminate duplicates. You'll need to loop the result in VBA to get them on one row separated by semicolons.

  5. #5
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,518
    Happy to help!
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

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

Similar Threads

  1. Replies: 6
    Last Post: 10-29-2018, 06:41 AM
  2. Find duplicates in three differenct tables
    By mlmelendez in forum Access
    Replies: 4
    Last Post: 06-28-2014, 03:22 PM
  3. Debugging a Select Case Statements (Still)
    By dccjr in forum Programming
    Replies: 13
    Last Post: 02-28-2013, 09:47 PM
  4. Combining Two INSERT INTO Statements
    By WyattR in forum Programming
    Replies: 4
    Last Post: 07-04-2012, 09:29 AM
  5. Replies: 1
    Last Post: 09-20-2011, 07:28 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