所以我有两个问题:
查询1:
SELECT
a.memno,
a.name,
a.addr1,
a.addr2,
a.city,
a.state,
a.zip,
a.sex,
a.lname,
a.ssan,
b.addr1 AS old_addr1,
b.addr2 AS old_addr2,
b.city AS old_city,
b.state AS old_state,
b.zip AS old_zip
FROM LIB1.TABLE1 a, LIB2.TABLE2 b
WHERE (a.memno = b.memno) AND
(b.groupid = 'P2') AND
(b.type = 'B') AND
(b.datec = 20131203) AND
(a.addr1 <> b.addr1) AND
(a.addr2 <> b.addr2) AND
(a.city <> b.city) AND
(a.state <> b.state) AND
(a.zip <> b.zip)
ORDER BY b.timec DESC
返回1条记录。
查询2:
SELECT
a.memno,
a.name,
a.addr1,
a.addr2,
a.city,
a.state,
a.zip,
a.sex,
a.lname,
a.ssan,
b.addr1 AS old_addr1,
b.addr2 AS old_addr2,
b.city AS old_city,
b.state AS old_state,
b.zip AS old_zip
FROM LIB2.TABLE1 a, LIB2.TABLE2 b
WHERE (a.memno = b.memno) AND
(b.groupid = 'N2') AND
(b.type = 'B') AND
(b.datec = 20131203) AND
(a.addr1 <> b.addr1) AND
(a.addr2 <> b.addr2) AND
(a.city <> b.city) AND
(a.state <> b.state) AND
(a.zip <> b.zip)
ORDER BY b.timec DESC
返回2条记录。
我正在尝试使用UNION让这两个查询返回1个结果集。
尝试UNION :
SELECT
a.memno,
a.name,
a.addr1,
a.addr2,
a.city,
a.state,
a.zip,
a.sex,
a.lname,
a.ssan,
b.addr1 AS old_addr1,
b.addr2 AS old_addr2,
b.city AS old_city,
b.state AS old_state,
b.zip AS old_zip
FROM LIB1.TABLE1 a, LIB2.TABLE2 b
WHERE (a.memno = b.memno) AND
(b.groupid = 'P2') AND
(b.type = 'B') AND
(b.datec = 20131203) AND
(a.addr1 <> b.addr1) AND
(a.addr2 <> b.addr2) AND
(a.city <> b.city) AND
(a.state <> b.state) AND
(a.zip <> b.zip)
UNION
SELECT
a.memno,
a.name,
a.addr1,
a.addr2,
a.city,
a.state,
a.zip,
a.sex,
a.lname,
a.ssan,
b.addr1 AS old_addr1,
b.addr2 AS old_addr2,
b.city AS old_city,
b.state AS old_state,
b.zip AS old_zip
FROM LIB2.TABLE1 a, LIB2.TABLE2 b
WHERE (a.memno = b.memno) AND
(b.groupid = 'N2') AND
(b.type = 'B') AND
(b.datec = 20131203) AND
(a.addr1 <> b.addr1) AND
(a.addr2 <> b.addr2) AND
(a.city <> b.city) AND
(a.state <> b.state) AND
(a.zip <> b.zip)
ORDER BY timec DESC
这给了我:
'DESC'附近的WHERE子句出错。 无法解析查询文本。
其次是:
SQL执行错误。
执行的SQL语句:SELECT a.memno,a.name,a.addr1,a.addr2,a.city,a.state,a.zip,a.sex,a.lname,a.ssan ,b.addr1 AS old_addr1,b.addr2 AS old_addr2,b.city AS old_city, b.state AS old_state,b.zip AS old_zip FROM LIB1.TABLE1 a,LIB2.TABLE2 b 在哪里(a.mem ......
错误来源:CWBODB.DLL
错误消息:ERROR [42000] [IBM] [System i Access ODBC Driver] [DB2 for i5 / OS] SQL0199 - 不期望关键字DESC。有效令牌:除了优化之外,跳过WTIH FETCH ORDER UNION。
修改
Bangs Head 无法相信我错过了我在第一次查询结束时仍然'desc'。在第一个查询结束时摆脱'desc'之后,我现在得到了同样的错误,但是消息:列TIMEC不能被限定。
EDIT2:
消息:ORDER BY列TIMEC或表达式不在结果表中。
答案 0 :(得分:3)
首次查询联合,您在最后一行的末尾留下了desc
:
WHERE a.memno [...snip...] <> b.zipc desc
^^^^
...这正是错误消息所说的。只是因为你可以在查询结尾看到(有效)DESC
,并不意味着在其他地方不能有desc
...当你的查询是一个时尤其如此几英里宽。
答案 1 :(得分:0)
对于您的第一个DESC
子句中的流氓WHERE
,ORDER BY
适用于生成的联合(没有timec
列),而不是源数据集。
您可以将两个查询合并为子查询:
SELECT * FROM
(SELECT a.memno, a.name,
a.addr1, a.addr2, a.city, a.state, a.zip, a.sex, a.lname, a.ssan,
b.addr1 as old_addr1, b.addr2 as old_addr2, b.city as old_city, b.state as old_state, b.zip as old_zip
FROM LIB1.TABLE1 a, LIB1.TABLE2 b
WHERE a.memno = b.memno
and b.groupid = 'P2'
and b.type = 'B'
and b.datec = 20131205
AND a.addr1 <> b.addr1
AND a.addr2 <> b.addr2
AND a.city <> b.city
AND a.state <> b.state
AND a.zip <> b.zipc
ORDER BY b.timec desc
) A
UNION
SELECT * FROM
(
SELECT a.memno, a.name,
a.addr1, a.addr2, a.city, a.state, a.zip, a.sex, a.lname, a.ssan,
b.addr1 as old_addr1, b.addr2 as old_addr2, b.city as old_city, b.state as old_state, b.zip as old_zip
FROM LIB2.TABLE1 a, LIB2.TABLE2 b
WHERE a.memno = b.memno
and b.groupid = 'N2'
and b.type = 'B'
and b.datec = 20131205
AND a.addr1 <> b.addr1
AND a.addr2 <> b.addr2
AND a.city <> b.city
AND a.state <> b.state
AND a.zip <> b.zip
ORDER BY b.timec desc
) B