SQL UNION query with 3 SELECTs

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Petrol
    Contributor
    • Oct 2016
    • 265

    SQL UNION query with 3 SELECTs

    Can anyone tell me what's wrong with the following UNION query? When I try to open it I get "Syntax error in FROM clause."
    I note that
    1. Each of the three SELECT queries runs (opens) quite happily when run by itself;
    2. If I remove one of them - any one - from the UNION query it runs happily;
    3. Grouping two of them in parentheses doesn't help;
    4. learn.microsoft .com says one can have mulltiple SELECT queries in a UNION query; and
    5. The embedded blank spaces after every 15 characters in the code below aren't there in the original; they' were inserted by Post.Bytes when I pasted the code, for some reason.
    Code:
     SELECT
    qry_LatestWalkD etails.PersonID ,
    qry_LatestWalkD etails.MRIText,
    qry_LatestWalkD etails.Effectiv eDate
    FROM
    qry_LatestWalkD etails
    UNION
    SELECT
    SetupQry_Latest BrdOrCteeDetail s.PersonID,
    SetupQry_Latest BrdOrCteeDetail s.MRIText,
    SetupQry_Latest BrdOrCteeDetail s.EffectiveDate
    FROM
    SetupQry_Latest BrdOrCteeDetail s
    UNION
    SELECT
    SetupQry_Latest SponsoringMRI.P ersonID,
    SetupQry_Latest SponsoringMRI.M RIText,
    SetupQry_Latest SponsoringMRI.E ffectiveDate
    FROM
    SetupQry_Latest SponsoringMRI
    ORDER BY PersonID;
  • natashasturrock
    New Member
    • Jul 2025
    • 17

    #2
    The error usually comes up in access when your query or table names have spaces. Just wrap them in square brackets like [SetupQry_Latest BrdOrCteeDetail s], and it should run fine.

    Comment

    • Petrol
      Contributor
      • Oct 2016
      • 265

      #3
      Thank you, Natasha. I tried that, but unfortunatelyu it didn't make any difference.
      Code:
      SELECT   [qry_LatestWalkDetails].[PersonID],
               [qry_LatestWalkDetails].[MRIText],
               [qry_LatestWalkDetails].[EffectiveDate]
      FROM     [qry_LatestWalkDetails]
      UNION
      SELECT   [SetupQry_LatestBrdOrCteeDetails].[PersonID],
               [SetupQry_LatestBrdOrCteeDetails].[MRIText],
               [SetupQry_LatestBrdOrCteeDetails].[EffectiveDate]
      FROM     [SetupQry_LatestBrdOrCteeDetails]
      UNION
      SELECT   [SetupQry_LatestSponsoringMRI].[PersonID],
               [SetupQry_LatestSponsoringMRI].[MRIText],
               [SetupQry_LatestSponsoringMRI].[EffectiveDate]
      FROM     [SetupQry_LatestSponsoringMRI]
      ORDER BY [PersonID];
      Last edited by NeoPa; 3 weeks ago. Reason: Fixed where some spaces were actually missing from your post.

      Comment

      • NeoPa
        Recognized Expert Moderator MVP
        • Oct 2006
        • 32645

        #4
        Hi Petrol.

        I found what you posted had actual extraneous spaces in it (as in that's what you put into the post not what Post.Bytes.com did to it), so I tidied that up a bit for you.

        Personally, I saw nothing actually wrong with any of the syntax but I would take advantage of a more succinct format to make it much easier to read & work with :
        Code:
        SELECT   [PersonID],
                 [MRIText],
                 [EffectiveDate]
        FROM     [qry_LatestWalkDetails]
        UNION
        SELECT   [PersonID],
                 [MRIText],
                 [EffectiveDate]
        FROM     [SetupQry_LatestBrdOrCteeDetails]
        UNION
        SELECT   [PersonID],
                 [MRIText],
                 [EffectiveDate]
        FROM     [SetupQry_LatestSponsoringMRI]
        ORDER BY [PersonID];
        Please try this & report as it's hard to know if what you last tried with had the spaces in or not. That is to say, if you copied & pasted your actual work straight into the post - as we all really should be doing when posting our work - then there were clearly unwanted spaces embedded in it.

        Comment

        • Petrol
          Contributor
          • Oct 2016
          • 265

          #5
          Thanks, NeoPa. I did try it. Query1, below, was copied and posted direct into the query design from your reply. As soon as I go to Datasheet view, it gives the same error:Click image for larger version

Name:	image.png
Views:	48
Size:	174.4 KB
ID:	5642187
          Did it not give the same result when you tried it?
          (And the unwanted spaces in the OP were/are NOT in my original. They were added by Post.Bytes after every 15th character when I pasted my query into the OP. I have found the same thing has happened with other posts into Post.Byes, although I never had that problem with the original Bytes.)

          Comment

          • NeoPa
            Recognized Expert Moderator MVP
            • Oct 2006
            • 32645

            #6
            Hi Petrol.

            I wasn't trying to be argumentative. The reason I said that is that I edited your actual post. What I found was that the spaces were in the text itself - not something added to the display by Post.Bytes.com. Possibly that occurred at an earlier stage & you copied that? Alternatively what I find in your actual source text isn't even the source text you put there (which would be extremely weird of course).

            NB. The original Bytes.com interface would sometimes add extraneous spaces into formatted code too :-( Quite irritating as it tends just to confuse & waste everybody's time.

            Obviously, I'm not in much of a position to try this out for you - not without setting up a whole new database for the privilege. That said, if you'd like me to do that for you I can. I'm not sure it would behave the same way but if you stay stuck I can do that & see? Let me know.

            Comment

            • CJ_London
              New Member
              • Nov 2013
              • 28

              #7
              Have you tried breaking it down into its components - run the first query, if works, union add the second query, check it works then add the third query

              You may need to use UNION ALL rather than just UNION which will remove duplicates - depends on your your requirements

              I know you said each query works OK but the error may be referring to the from clause in your separate queries

              Comment

              • Petrol
                Contributor
                • Oct 2016
                • 265

                #8
                Sorry, CJ_London, I missed responding to your suggestion. None of the above suggestions seemed to work, so I ended up splitting it into two union queries each combining only two Select queries.
                Thanks to all anyway.

                Comment

                Working...