| |
|
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.
|
 |

04-14-09, 14:29
|
|
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
|
|

04-14-09, 14:54
|
|
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
|
|

04-14-09, 15:39
|
|
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.
|
|

04-14-09, 16:30
|
|
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,
|
|

04-14-09, 16:45
|
|
Registered User
|
|
Join Date: Jan 2003
Posts: 3,575
|
|
Then I would suspect that table M has no column C.
Andy
|
|

04-14-09, 16:58
|
|
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)
|
|

04-15-09, 08:16
|
|
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
|
|

04-15-09, 13:11
|
|
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.
|
|

04-15-09, 13:16
|
|
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
|
|

04-15-09, 14:08
|
|
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.
|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|