Why won't UNION work within CTE or subquery?












1















I'm trying to run a query via System i Navigator that selects from the combined result of a union.



This works fine:



SELECT AF15VC FROM DB.AF
UNION
SELECT AF15VC FROM BATCH.AFM


So why doesn't this work?



WITH CTE AS (
SELECT AF15VC FROM DB.AF
UNION
SELECT AF15VC FROM BATCH.AFM
)
SELECT *
FROM CTE


And why doesn't this work?



SELECT *
FROM
(SELECT AF15VC FROM DB.AF
UNION
SELECT AF15VC FROM BATCH.AFM
) AS AF


In both cases, I get this error:




SQL State: 42601



Vendor Code: -199



Message: [SQL0199]



Keyword UNION
not expected. Valid tokens: ). Cause . . . . . : The keyword UNION
was not expected here. A syntax error was detected at keyword UNION.
The partial list of valid tokens is ). This list assumes that the
statement is correct up to the unexpected keyword. The error may be
earlier in the statement but the syntax of the statement seems to be
valid up to this point. Recovery . . . : Examine the SQL statement
in the area of the specified keyword. A colon or SQL delimiter may be
missing. SQL requires reserved words to be delimited when they are
used as a name. Correct the SQL statement and try the request again.




I tried UNION ALL as well with no change in the result.



Update:



Since people keep thinking I'm not showing the actual queries, I added screenshots. Here is the first query working fine and the second two failing:



query 1query 2query 3










share|improve this question

























  • Have you tried adding ;?

    – Lukasz Szozda
    Jan 3 at 18:19













  • @LukaszSzozda yes. I also tried removing AS and AS AF.

    – MarredCheese
    Jan 3 at 18:20











  • Ok, but is this single statement or part of script/stored procedure?

    – Lukasz Szozda
    Jan 3 at 18:20













  • @LukaszSzozda single statement

    – MarredCheese
    Jan 3 at 18:22











  • Interesting, both of these syntaxes work for me. I am at v7.2, what release are you on?

    – jmarkmurphy
    Jan 3 at 19:07
















1















I'm trying to run a query via System i Navigator that selects from the combined result of a union.



This works fine:



SELECT AF15VC FROM DB.AF
UNION
SELECT AF15VC FROM BATCH.AFM


So why doesn't this work?



WITH CTE AS (
SELECT AF15VC FROM DB.AF
UNION
SELECT AF15VC FROM BATCH.AFM
)
SELECT *
FROM CTE


And why doesn't this work?



SELECT *
FROM
(SELECT AF15VC FROM DB.AF
UNION
SELECT AF15VC FROM BATCH.AFM
) AS AF


In both cases, I get this error:




SQL State: 42601



Vendor Code: -199



Message: [SQL0199]



Keyword UNION
not expected. Valid tokens: ). Cause . . . . . : The keyword UNION
was not expected here. A syntax error was detected at keyword UNION.
The partial list of valid tokens is ). This list assumes that the
statement is correct up to the unexpected keyword. The error may be
earlier in the statement but the syntax of the statement seems to be
valid up to this point. Recovery . . . : Examine the SQL statement
in the area of the specified keyword. A colon or SQL delimiter may be
missing. SQL requires reserved words to be delimited when they are
used as a name. Correct the SQL statement and try the request again.




I tried UNION ALL as well with no change in the result.



Update:



Since people keep thinking I'm not showing the actual queries, I added screenshots. Here is the first query working fine and the second two failing:



query 1query 2query 3










share|improve this question

























  • Have you tried adding ;?

    – Lukasz Szozda
    Jan 3 at 18:19













  • @LukaszSzozda yes. I also tried removing AS and AS AF.

    – MarredCheese
    Jan 3 at 18:20











  • Ok, but is this single statement or part of script/stored procedure?

    – Lukasz Szozda
    Jan 3 at 18:20













  • @LukaszSzozda single statement

    – MarredCheese
    Jan 3 at 18:22











  • Interesting, both of these syntaxes work for me. I am at v7.2, what release are you on?

    – jmarkmurphy
    Jan 3 at 19:07














1












1








1








I'm trying to run a query via System i Navigator that selects from the combined result of a union.



This works fine:



SELECT AF15VC FROM DB.AF
UNION
SELECT AF15VC FROM BATCH.AFM


So why doesn't this work?



WITH CTE AS (
SELECT AF15VC FROM DB.AF
UNION
SELECT AF15VC FROM BATCH.AFM
)
SELECT *
FROM CTE


And why doesn't this work?



SELECT *
FROM
(SELECT AF15VC FROM DB.AF
UNION
SELECT AF15VC FROM BATCH.AFM
) AS AF


In both cases, I get this error:




SQL State: 42601



Vendor Code: -199



Message: [SQL0199]



Keyword UNION
not expected. Valid tokens: ). Cause . . . . . : The keyword UNION
was not expected here. A syntax error was detected at keyword UNION.
The partial list of valid tokens is ). This list assumes that the
statement is correct up to the unexpected keyword. The error may be
earlier in the statement but the syntax of the statement seems to be
valid up to this point. Recovery . . . : Examine the SQL statement
in the area of the specified keyword. A colon or SQL delimiter may be
missing. SQL requires reserved words to be delimited when they are
used as a name. Correct the SQL statement and try the request again.




I tried UNION ALL as well with no change in the result.



Update:



Since people keep thinking I'm not showing the actual queries, I added screenshots. Here is the first query working fine and the second two failing:



query 1query 2query 3










share|improve this question
















I'm trying to run a query via System i Navigator that selects from the combined result of a union.



This works fine:



SELECT AF15VC FROM DB.AF
UNION
SELECT AF15VC FROM BATCH.AFM


So why doesn't this work?



WITH CTE AS (
SELECT AF15VC FROM DB.AF
UNION
SELECT AF15VC FROM BATCH.AFM
)
SELECT *
FROM CTE


And why doesn't this work?



SELECT *
FROM
(SELECT AF15VC FROM DB.AF
UNION
SELECT AF15VC FROM BATCH.AFM
) AS AF


In both cases, I get this error:




SQL State: 42601



Vendor Code: -199



Message: [SQL0199]



Keyword UNION
not expected. Valid tokens: ). Cause . . . . . : The keyword UNION
was not expected here. A syntax error was detected at keyword UNION.
The partial list of valid tokens is ). This list assumes that the
statement is correct up to the unexpected keyword. The error may be
earlier in the statement but the syntax of the statement seems to be
valid up to this point. Recovery . . . : Examine the SQL statement
in the area of the specified keyword. A colon or SQL delimiter may be
missing. SQL requires reserved words to be delimited when they are
used as a name. Correct the SQL statement and try the request again.




I tried UNION ALL as well with no change in the result.



Update:



Since people keep thinking I'm not showing the actual queries, I added screenshots. Here is the first query working fine and the second two failing:



query 1query 2query 3







sql union ibm-midrange db2-400






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 3 at 22:37







MarredCheese

















asked Jan 3 at 18:13









MarredCheeseMarredCheese

3,09112239




3,09112239













  • Have you tried adding ;?

    – Lukasz Szozda
    Jan 3 at 18:19













  • @LukaszSzozda yes. I also tried removing AS and AS AF.

    – MarredCheese
    Jan 3 at 18:20











  • Ok, but is this single statement or part of script/stored procedure?

    – Lukasz Szozda
    Jan 3 at 18:20













  • @LukaszSzozda single statement

    – MarredCheese
    Jan 3 at 18:22











  • Interesting, both of these syntaxes work for me. I am at v7.2, what release are you on?

    – jmarkmurphy
    Jan 3 at 19:07



















  • Have you tried adding ;?

    – Lukasz Szozda
    Jan 3 at 18:19













  • @LukaszSzozda yes. I also tried removing AS and AS AF.

    – MarredCheese
    Jan 3 at 18:20











  • Ok, but is this single statement or part of script/stored procedure?

    – Lukasz Szozda
    Jan 3 at 18:20













  • @LukaszSzozda single statement

    – MarredCheese
    Jan 3 at 18:22











  • Interesting, both of these syntaxes work for me. I am at v7.2, what release are you on?

    – jmarkmurphy
    Jan 3 at 19:07

















Have you tried adding ;?

– Lukasz Szozda
Jan 3 at 18:19







Have you tried adding ;?

– Lukasz Szozda
Jan 3 at 18:19















@LukaszSzozda yes. I also tried removing AS and AS AF.

– MarredCheese
Jan 3 at 18:20





@LukaszSzozda yes. I also tried removing AS and AS AF.

– MarredCheese
Jan 3 at 18:20













Ok, but is this single statement or part of script/stored procedure?

– Lukasz Szozda
Jan 3 at 18:20







Ok, but is this single statement or part of script/stored procedure?

– Lukasz Szozda
Jan 3 at 18:20















@LukaszSzozda single statement

– MarredCheese
Jan 3 at 18:22





@LukaszSzozda single statement

– MarredCheese
Jan 3 at 18:22













Interesting, both of these syntaxes work for me. I am at v7.2, what release are you on?

– jmarkmurphy
Jan 3 at 19:07





Interesting, both of these syntaxes work for me. I am at v7.2, what release are you on?

– jmarkmurphy
Jan 3 at 19:07












3 Answers
3






active

oldest

votes


















1














Works fine for me on release 7.2



with cte as (
select pmco#, pmmanf
from dtdata.pdpmast
union
select pmco#, pmmanf
from devqdata.pdpmast
)
select * from cte;


Note: use UNION ALL if you don't have or don't care about duplicates. UNION gets rid of duplicates and if there aren't any means a lot of wasted processing.






share|improve this answer


























  • The real query is more complex. I'm trying to do a query where the source is 4 tables that are identical in structure but contain data from different time periods. Someone before me achieved this by doing this: {query on table 1} union {identical query on table 2} etc... To eliminate the 3 copied-and-pasted queries, I'd rather do this instead if possible: query on {table 1 union table2 etc...}.

    – MarredCheese
    Jan 3 at 18:48








  • 1





    While it is correct that a fullselect is a combination of subselects, the table-clause (at least at v7.1 and later, and I believe much earlier). can contain a nested table expression which looks like [LATERAL] ( fullsellect ) [correlation-clause].

    – jmarkmurphy
    Jan 3 at 19:06








  • 1





    I was able to look at a v5r3 manual, and even there the CTE syntax is table-identifier [(column-name, ...)] AS ( fullselect [order-by-clause] [fetch-clause] ). The nested-table-expression is similar except in the docs at v7.1 and earlier, the correlation-clause is a required component. And by test, at v7.2, the correlation-clause is still required. It could have been made optional at some later TR in v7.2.

    – jmarkmurphy
    Jan 3 at 19:22













  • @jmarkmurphy is correct, just tried it on 7.2 and it works fine. I'll edit my answer.

    – Charles
    Jan 3 at 20:30





















1














Your statements should work fine. Here is a test from my IBM i at v7.2, but it should work the same at v7.1, or at least all the way back to v5r3 based on the documentation that I can find.



create table tablea
(field1 Char(10),
field2 Char(10));
create table tableb
(field1 Char(10),
field2 Char(10));
insert into tablea
values ('row1', 'mama'),
('row2', 'papa');
insert into tableb
values ('rowa', 'timmy'),
('rowb', 'sissy');


then



select * from tablea
union
select * from tableb;


gives:




FIELD1 FIELD2
------------------------
row1 mama
rowb sissy
row2 papa
rowa timmy


and



with cte as (
select * from tablea
union
select * from tableb)
select * from cte;


gives




FIELD1 FIELD2
------------------------
rowb sissy
row1 mama
row2 papa
rowa timmy


and



select * 
from (
select * from tablea
union
select * from tableb) a


gives




FIELD1 FIELD2
------------------------
rowb sissy
row1 mama
row2 papa
rowa timmy





share|improve this answer































    1














    Well, apparently the system is actually version 5.1, not 7.1 as I had stated in a comment. I mistook my version of System i Navigator for the database engine version (or OS, or whatever it is). As far as I know, the really old version is the problem.






    share|improve this answer
























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54027644%2fwhy-wont-union-work-within-cte-or-subquery%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      Works fine for me on release 7.2



      with cte as (
      select pmco#, pmmanf
      from dtdata.pdpmast
      union
      select pmco#, pmmanf
      from devqdata.pdpmast
      )
      select * from cte;


      Note: use UNION ALL if you don't have or don't care about duplicates. UNION gets rid of duplicates and if there aren't any means a lot of wasted processing.






      share|improve this answer


























      • The real query is more complex. I'm trying to do a query where the source is 4 tables that are identical in structure but contain data from different time periods. Someone before me achieved this by doing this: {query on table 1} union {identical query on table 2} etc... To eliminate the 3 copied-and-pasted queries, I'd rather do this instead if possible: query on {table 1 union table2 etc...}.

        – MarredCheese
        Jan 3 at 18:48








      • 1





        While it is correct that a fullselect is a combination of subselects, the table-clause (at least at v7.1 and later, and I believe much earlier). can contain a nested table expression which looks like [LATERAL] ( fullsellect ) [correlation-clause].

        – jmarkmurphy
        Jan 3 at 19:06








      • 1





        I was able to look at a v5r3 manual, and even there the CTE syntax is table-identifier [(column-name, ...)] AS ( fullselect [order-by-clause] [fetch-clause] ). The nested-table-expression is similar except in the docs at v7.1 and earlier, the correlation-clause is a required component. And by test, at v7.2, the correlation-clause is still required. It could have been made optional at some later TR in v7.2.

        – jmarkmurphy
        Jan 3 at 19:22













      • @jmarkmurphy is correct, just tried it on 7.2 and it works fine. I'll edit my answer.

        – Charles
        Jan 3 at 20:30


















      1














      Works fine for me on release 7.2



      with cte as (
      select pmco#, pmmanf
      from dtdata.pdpmast
      union
      select pmco#, pmmanf
      from devqdata.pdpmast
      )
      select * from cte;


      Note: use UNION ALL if you don't have or don't care about duplicates. UNION gets rid of duplicates and if there aren't any means a lot of wasted processing.






      share|improve this answer


























      • The real query is more complex. I'm trying to do a query where the source is 4 tables that are identical in structure but contain data from different time periods. Someone before me achieved this by doing this: {query on table 1} union {identical query on table 2} etc... To eliminate the 3 copied-and-pasted queries, I'd rather do this instead if possible: query on {table 1 union table2 etc...}.

        – MarredCheese
        Jan 3 at 18:48








      • 1





        While it is correct that a fullselect is a combination of subselects, the table-clause (at least at v7.1 and later, and I believe much earlier). can contain a nested table expression which looks like [LATERAL] ( fullsellect ) [correlation-clause].

        – jmarkmurphy
        Jan 3 at 19:06








      • 1





        I was able to look at a v5r3 manual, and even there the CTE syntax is table-identifier [(column-name, ...)] AS ( fullselect [order-by-clause] [fetch-clause] ). The nested-table-expression is similar except in the docs at v7.1 and earlier, the correlation-clause is a required component. And by test, at v7.2, the correlation-clause is still required. It could have been made optional at some later TR in v7.2.

        – jmarkmurphy
        Jan 3 at 19:22













      • @jmarkmurphy is correct, just tried it on 7.2 and it works fine. I'll edit my answer.

        – Charles
        Jan 3 at 20:30
















      1












      1








      1







      Works fine for me on release 7.2



      with cte as (
      select pmco#, pmmanf
      from dtdata.pdpmast
      union
      select pmco#, pmmanf
      from devqdata.pdpmast
      )
      select * from cte;


      Note: use UNION ALL if you don't have or don't care about duplicates. UNION gets rid of duplicates and if there aren't any means a lot of wasted processing.






      share|improve this answer















      Works fine for me on release 7.2



      with cte as (
      select pmco#, pmmanf
      from dtdata.pdpmast
      union
      select pmco#, pmmanf
      from devqdata.pdpmast
      )
      select * from cte;


      Note: use UNION ALL if you don't have or don't care about duplicates. UNION gets rid of duplicates and if there aren't any means a lot of wasted processing.







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Jan 3 at 20:33

























      answered Jan 3 at 18:29









      CharlesCharles

      11.3k11132




      11.3k11132













      • The real query is more complex. I'm trying to do a query where the source is 4 tables that are identical in structure but contain data from different time periods. Someone before me achieved this by doing this: {query on table 1} union {identical query on table 2} etc... To eliminate the 3 copied-and-pasted queries, I'd rather do this instead if possible: query on {table 1 union table2 etc...}.

        – MarredCheese
        Jan 3 at 18:48








      • 1





        While it is correct that a fullselect is a combination of subselects, the table-clause (at least at v7.1 and later, and I believe much earlier). can contain a nested table expression which looks like [LATERAL] ( fullsellect ) [correlation-clause].

        – jmarkmurphy
        Jan 3 at 19:06








      • 1





        I was able to look at a v5r3 manual, and even there the CTE syntax is table-identifier [(column-name, ...)] AS ( fullselect [order-by-clause] [fetch-clause] ). The nested-table-expression is similar except in the docs at v7.1 and earlier, the correlation-clause is a required component. And by test, at v7.2, the correlation-clause is still required. It could have been made optional at some later TR in v7.2.

        – jmarkmurphy
        Jan 3 at 19:22













      • @jmarkmurphy is correct, just tried it on 7.2 and it works fine. I'll edit my answer.

        – Charles
        Jan 3 at 20:30





















      • The real query is more complex. I'm trying to do a query where the source is 4 tables that are identical in structure but contain data from different time periods. Someone before me achieved this by doing this: {query on table 1} union {identical query on table 2} etc... To eliminate the 3 copied-and-pasted queries, I'd rather do this instead if possible: query on {table 1 union table2 etc...}.

        – MarredCheese
        Jan 3 at 18:48








      • 1





        While it is correct that a fullselect is a combination of subselects, the table-clause (at least at v7.1 and later, and I believe much earlier). can contain a nested table expression which looks like [LATERAL] ( fullsellect ) [correlation-clause].

        – jmarkmurphy
        Jan 3 at 19:06








      • 1





        I was able to look at a v5r3 manual, and even there the CTE syntax is table-identifier [(column-name, ...)] AS ( fullselect [order-by-clause] [fetch-clause] ). The nested-table-expression is similar except in the docs at v7.1 and earlier, the correlation-clause is a required component. And by test, at v7.2, the correlation-clause is still required. It could have been made optional at some later TR in v7.2.

        – jmarkmurphy
        Jan 3 at 19:22













      • @jmarkmurphy is correct, just tried it on 7.2 and it works fine. I'll edit my answer.

        – Charles
        Jan 3 at 20:30



















      The real query is more complex. I'm trying to do a query where the source is 4 tables that are identical in structure but contain data from different time periods. Someone before me achieved this by doing this: {query on table 1} union {identical query on table 2} etc... To eliminate the 3 copied-and-pasted queries, I'd rather do this instead if possible: query on {table 1 union table2 etc...}.

      – MarredCheese
      Jan 3 at 18:48







      The real query is more complex. I'm trying to do a query where the source is 4 tables that are identical in structure but contain data from different time periods. Someone before me achieved this by doing this: {query on table 1} union {identical query on table 2} etc... To eliminate the 3 copied-and-pasted queries, I'd rather do this instead if possible: query on {table 1 union table2 etc...}.

      – MarredCheese
      Jan 3 at 18:48






      1




      1





      While it is correct that a fullselect is a combination of subselects, the table-clause (at least at v7.1 and later, and I believe much earlier). can contain a nested table expression which looks like [LATERAL] ( fullsellect ) [correlation-clause].

      – jmarkmurphy
      Jan 3 at 19:06







      While it is correct that a fullselect is a combination of subselects, the table-clause (at least at v7.1 and later, and I believe much earlier). can contain a nested table expression which looks like [LATERAL] ( fullsellect ) [correlation-clause].

      – jmarkmurphy
      Jan 3 at 19:06






      1




      1





      I was able to look at a v5r3 manual, and even there the CTE syntax is table-identifier [(column-name, ...)] AS ( fullselect [order-by-clause] [fetch-clause] ). The nested-table-expression is similar except in the docs at v7.1 and earlier, the correlation-clause is a required component. And by test, at v7.2, the correlation-clause is still required. It could have been made optional at some later TR in v7.2.

      – jmarkmurphy
      Jan 3 at 19:22







      I was able to look at a v5r3 manual, and even there the CTE syntax is table-identifier [(column-name, ...)] AS ( fullselect [order-by-clause] [fetch-clause] ). The nested-table-expression is similar except in the docs at v7.1 and earlier, the correlation-clause is a required component. And by test, at v7.2, the correlation-clause is still required. It could have been made optional at some later TR in v7.2.

      – jmarkmurphy
      Jan 3 at 19:22















      @jmarkmurphy is correct, just tried it on 7.2 and it works fine. I'll edit my answer.

      – Charles
      Jan 3 at 20:30







      @jmarkmurphy is correct, just tried it on 7.2 and it works fine. I'll edit my answer.

      – Charles
      Jan 3 at 20:30















      1














      Your statements should work fine. Here is a test from my IBM i at v7.2, but it should work the same at v7.1, or at least all the way back to v5r3 based on the documentation that I can find.



      create table tablea
      (field1 Char(10),
      field2 Char(10));
      create table tableb
      (field1 Char(10),
      field2 Char(10));
      insert into tablea
      values ('row1', 'mama'),
      ('row2', 'papa');
      insert into tableb
      values ('rowa', 'timmy'),
      ('rowb', 'sissy');


      then



      select * from tablea
      union
      select * from tableb;


      gives:




      FIELD1 FIELD2
      ------------------------
      row1 mama
      rowb sissy
      row2 papa
      rowa timmy


      and



      with cte as (
      select * from tablea
      union
      select * from tableb)
      select * from cte;


      gives




      FIELD1 FIELD2
      ------------------------
      rowb sissy
      row1 mama
      row2 papa
      rowa timmy


      and



      select * 
      from (
      select * from tablea
      union
      select * from tableb) a


      gives




      FIELD1 FIELD2
      ------------------------
      rowb sissy
      row1 mama
      row2 papa
      rowa timmy





      share|improve this answer




























        1














        Your statements should work fine. Here is a test from my IBM i at v7.2, but it should work the same at v7.1, or at least all the way back to v5r3 based on the documentation that I can find.



        create table tablea
        (field1 Char(10),
        field2 Char(10));
        create table tableb
        (field1 Char(10),
        field2 Char(10));
        insert into tablea
        values ('row1', 'mama'),
        ('row2', 'papa');
        insert into tableb
        values ('rowa', 'timmy'),
        ('rowb', 'sissy');


        then



        select * from tablea
        union
        select * from tableb;


        gives:




        FIELD1 FIELD2
        ------------------------
        row1 mama
        rowb sissy
        row2 papa
        rowa timmy


        and



        with cte as (
        select * from tablea
        union
        select * from tableb)
        select * from cte;


        gives




        FIELD1 FIELD2
        ------------------------
        rowb sissy
        row1 mama
        row2 papa
        rowa timmy


        and



        select * 
        from (
        select * from tablea
        union
        select * from tableb) a


        gives




        FIELD1 FIELD2
        ------------------------
        rowb sissy
        row1 mama
        row2 papa
        rowa timmy





        share|improve this answer


























          1












          1








          1







          Your statements should work fine. Here is a test from my IBM i at v7.2, but it should work the same at v7.1, or at least all the way back to v5r3 based on the documentation that I can find.



          create table tablea
          (field1 Char(10),
          field2 Char(10));
          create table tableb
          (field1 Char(10),
          field2 Char(10));
          insert into tablea
          values ('row1', 'mama'),
          ('row2', 'papa');
          insert into tableb
          values ('rowa', 'timmy'),
          ('rowb', 'sissy');


          then



          select * from tablea
          union
          select * from tableb;


          gives:




          FIELD1 FIELD2
          ------------------------
          row1 mama
          rowb sissy
          row2 papa
          rowa timmy


          and



          with cte as (
          select * from tablea
          union
          select * from tableb)
          select * from cte;


          gives




          FIELD1 FIELD2
          ------------------------
          rowb sissy
          row1 mama
          row2 papa
          rowa timmy


          and



          select * 
          from (
          select * from tablea
          union
          select * from tableb) a


          gives




          FIELD1 FIELD2
          ------------------------
          rowb sissy
          row1 mama
          row2 papa
          rowa timmy





          share|improve this answer













          Your statements should work fine. Here is a test from my IBM i at v7.2, but it should work the same at v7.1, or at least all the way back to v5r3 based on the documentation that I can find.



          create table tablea
          (field1 Char(10),
          field2 Char(10));
          create table tableb
          (field1 Char(10),
          field2 Char(10));
          insert into tablea
          values ('row1', 'mama'),
          ('row2', 'papa');
          insert into tableb
          values ('rowa', 'timmy'),
          ('rowb', 'sissy');


          then



          select * from tablea
          union
          select * from tableb;


          gives:




          FIELD1 FIELD2
          ------------------------
          row1 mama
          rowb sissy
          row2 papa
          rowa timmy


          and



          with cte as (
          select * from tablea
          union
          select * from tableb)
          select * from cte;


          gives




          FIELD1 FIELD2
          ------------------------
          rowb sissy
          row1 mama
          row2 papa
          rowa timmy


          and



          select * 
          from (
          select * from tablea
          union
          select * from tableb) a


          gives




          FIELD1 FIELD2
          ------------------------
          rowb sissy
          row1 mama
          row2 papa
          rowa timmy






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 3 at 20:36









          jmarkmurphyjmarkmurphy

          8,0902141




          8,0902141























              1














              Well, apparently the system is actually version 5.1, not 7.1 as I had stated in a comment. I mistook my version of System i Navigator for the database engine version (or OS, or whatever it is). As far as I know, the really old version is the problem.






              share|improve this answer




























                1














                Well, apparently the system is actually version 5.1, not 7.1 as I had stated in a comment. I mistook my version of System i Navigator for the database engine version (or OS, or whatever it is). As far as I know, the really old version is the problem.






                share|improve this answer


























                  1












                  1








                  1







                  Well, apparently the system is actually version 5.1, not 7.1 as I had stated in a comment. I mistook my version of System i Navigator for the database engine version (or OS, or whatever it is). As far as I know, the really old version is the problem.






                  share|improve this answer













                  Well, apparently the system is actually version 5.1, not 7.1 as I had stated in a comment. I mistook my version of System i Navigator for the database engine version (or OS, or whatever it is). As far as I know, the really old version is the problem.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jan 16 at 21:16









                  MarredCheeseMarredCheese

                  3,09112239




                  3,09112239






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54027644%2fwhy-wont-union-work-within-cte-or-subquery%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Mossoró

                      Error while reading .h5 file using the rhdf5 package in R

                      Pushsharp Apns notification error: 'InvalidToken'