If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > Union inside select

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-01-04, 11:53
atmosphere atmosphere is offline
Registered User
 
Join Date: Aug 2004
Posts: 19
Union inside select

Hi

Im trying to select from a table that uses a union with little sucess. i get an sql syntax error.





SELECT

something, something_else

FROM
(

SELECT ....

UNION ALL

SELECT ....

)

NOTE: Everything inside the FROM clause works fine on its own.
Reply With Quote
  #2 (permalink)  
Old 09-01-04, 12:28
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
Try it like this:


WITH temp1 as
(

SELECT ....

UNION ALL

SELECT ....

)
SELECT

something, something_else from temp1

Andy
Reply With Quote
  #3 (permalink)  
Old 09-01-04, 12:54
atmosphere atmosphere is offline
Registered User
 
Join Date: Aug 2004
Posts: 19
I am getting a syntax error on that
Reply With Quote
  #4 (permalink)  
Old 09-01-04, 13:24
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
Please post your entire query.
Andy
Reply With Quote
  #5 (permalink)  
Old 09-01-04, 18:49
sathyaram_s sathyaram_s is offline
Super Moderator
 
Join Date: Aug 2001
Location: UK
Posts: 4,534
and your platform and db2 version...

CTE( WITH Construct) is not supported on mainframe till v7

Cheers
sathyaram

Quote:
Originally Posted by ARWinner
Please post your entire query.
Andy
__________________
Visit the new-look IDUG Website , register to gain access to the excellent content.
Reply With Quote
  #6 (permalink)  
Old 09-02-04, 04:11
Damian Ibbotson Damian Ibbotson is offline
Padawan
 
Join Date: Jun 2002
Location: UK
Posts: 525
It's getting confused and thinks you're attempting to write a recursive statment.

Try using the TABLE keyword...

Code:
select cols
from table (
            select cols
            from table1
             union all
            select cols
            from table2
           ) tmpTable
;
Also, think about using UNION ALL in preference to UNION unless there is a good reason to distinct your select.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On