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 > A query Join the union result

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-14-09, 14:29
newer newer is offline
Registered User
 
Join Date: Sep 2003
Posts: 32
A query Join the union result

SELECT a, M.c
FROM
(SELECT d AS a
FROM X
UNION ALL
SELECT e AS a
FROM Y) Z, M
WHERE Z.a = M.b

Give me error “Column c or expression in SELECT list not valid.”

What is the problem?
Thanks,

Lida
Reply With Quote
  #2 (permalink)  
Old 04-14-09, 14:54
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
Use a CTE:

with t1 (a) as (SELECT d AS a
FROM X
UNION ALL
SELECT e AS a
FROM Y) select t.a,m.c from t1 as t inner join m as m on (t.a = m.b)

Andy
Reply With Quote
  #3 (permalink)  
Old 04-14-09, 15:39
tonkuma tonkuma is offline
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
What platform and DB2 version/release are you using?

Your query worked on my DB2 V9.5 on Windows XP.
Code:
------------------------------ Commands Entered ------------------------------
connect to SAMPLE ;
------------------------------------------------------------------------------

   Database Connection Information

 Database server        = DB2/NT 9.5.2
 SQL authorization ID   = DB2ADMIN
 Local database alias   = SAMPLE


A JDBC connection to the target has succeeded.
------------------------------ Commands Entered ------------------------------
WITH
 X(d) AS (VALUES
1, 2, 5
)
,Y(e) AS (VALUES
1, 3, 4, 5
)
,M(b, c) AS (VALUES
 (1, 'aaa')
,(2, 'bbb')
,(3, 'ccc')
)
SELECT a, M.c
FROM 
(SELECT d AS a
FROM X
UNION ALL
SELECT e AS a
FROM Y) Z, M
WHERE Z.a = M.b
;
------------------------------------------------------------------------------

A           C  
----------- ---
          1 aaa
          3 ccc
          1 aaa
          2 bbb

  4 record(s) selected.
Reply With Quote
  #4 (permalink)  
Old 04-14-09, 16:30
newer newer is offline
Registered User
 
Join Date: Sep 2003
Posts: 32
Join Union result

Hi Andy,

I got the same error: "Column c or expression in SELECT list not valid"
Code:
with t1 (a) as (SELECT d AS a
FROM X
UNION ALL
SELECT e AS a
FROM Y) select t.a,m.c from t1 as t inner join m as m on (t.a = m.b)
My version is AS400 v5r4, I use ODBC to query.

Thanks,
Reply With Quote
  #5 (permalink)  
Old 04-14-09, 16:45
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
Then I would suspect that table M has no column C.

Andy
Reply With Quote
  #6 (permalink)  
Old 04-14-09, 16:58
newer newer is offline
Registered User
 
Join Date: Sep 2003
Posts: 32
I even tried: (select m.b, which is the join field)
with t1 (a) as (SELECT d AS a
FROM X
UNION ALL
SELECT e AS a
FROM Y) select t.a,m.b from t1 as t inner join m as m on (t.a = m.b)
Reply With Quote
  #7 (permalink)  
Old 04-15-09, 08:16
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
Quote:
Originally Posted by newer
I even tried: (select m.b, which is the join field)
with t1 (a) as (SELECT d AS a
FROM X
UNION ALL
SELECT e AS a
FROM Y) select t.a,m.b from t1 as t inner join m as m on (t.a = m.b)
And what does this give you?

Andy
Reply With Quote
  #8 (permalink)  
Old 04-15-09, 13:11
newer newer is offline
Registered User
 
Join Date: Sep 2003
Posts: 32
Andy,
The same error. I am sure there is no problem with field (or column) name. There is no problem if I join first then Union, but the performance will be better if Union first then Join since I will only need top 5. So don't think there is any problem with column name. Actually, the query you post has no difference with my original query.
Reply With Quote
  #9 (permalink)  
Old 04-15-09, 13:16
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
How can it give you the same error if you do not reference column C? Can you post the entire error message complete with SQLCODE and SQLSTATE?

Andy
Reply With Quote
  #10 (permalink)  
Old 04-15-09, 14:08
newer newer is offline
Registered User
 
Join Date: Sep 2003
Posts: 32
ok, The error message complaint about column m.b if I select column m.b.

I almost don't think there is any problem with Query. It is because of version. tonkuma even tested my query with no problem. Thank you any way.
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