请参阅下面我正在努力的代码:
select *
from AAA_PROD_XEUSS.P_E_LVR_CQT CQT
left join AAA_PROD_XEUSS.P_F_OPERATION OPER
on CQT.OPERATION = OPER.OPERATION
union
select *
from BBB_PROD_XEUSS.P_E_LVR_CQT CQT
left join BBB_PROD_XEUSS.P_F_OPERATION OPER
on CQT.OPERATION = OPER.OPERATION
where CQT.PROCESS = '1111'
Order by OPERATION
我收到以下错误: 错误[42877] [IBM] [DB2 / LINUXX8664] SQL0197N ORDER BY子句中不允许使用限定列名。 任何提出错误的建议解决方案
谢谢
答案 0 :(得分:0)
在外面使用order by子句,并为内部部分的选择列表中的所有列添加别名。
( cqt.operation
或oper.operation
中的一个可能在内部首选,因为它们匹配为相等):
Select *
from
(select cqt.col1, cqt.col2, oper.*
-- where "operation" column is exempted from cqt list,
-- since the table aliased as oper already has "operation" column.
from AAA_PROD_XEUSS.P_E_LVR_CQT CQT
left join AAA_PROD_XEUSS.P_F_OPERATION OPER on CQT.OPERATION = OPER.OPERATION
union
select cqt.col1, cqt.col2, oper.*
from BBB_PROD_XEUSS.P_E_LVR_CQT CQT
left join BBB_PROD_XEUSS.P_F_OPERATION OPER on CQT.OPERATION = OPER.OPERATION
where CQT.PROCESS = '1111'
)
Order by OPERATION
答案 1 :(得分:0)
您可以使用order by
,但需要明确列出列。无论如何,这是一个很好的做法:
select cqt.*, oper.?, oper.?, . . .
from AAA_PROD_XEUSS.P_E_LVR_CQT CQT left join
AAA_PROD_XEUSS.P_F_OPERATION OPER
on CQT.OPERATION = OPER.OPERATION
union
select cqt.*, oper.?, oper.?, . . .
from BBB_PROD_XEUSS.P_E_LVR_CQT CQT left join
BBB_PROD_XEUSS.P_F_OPERATION OPER
on CQT.OPERATION = OPER.OPERATION
where CQT.PROCESS = '1111'
Order by OPERATION;
OPERATION
列应来自CQT
表。 ?
是OPER
中其他列的占位符。这假设两个CQT
表具有相同的列,并且具有以相同顺序定义的相同类型。更好的做法是列出所有列。
答案 2 :(得分:0)
如果您使用的是Db2 11.1,则可以使用JOIN USING
语法来避免在结果集https://www.ibm.com/support/knowledgecenter/en/SSEPGG_11.1.0/com.ibm.db2.luw.sql.ref.doc/doc/r0059207.html
E.g。这些表格
create table AAA_PROD_XEUSS.P_E_LVR_CQT (i int, OPERATION int, PROCESS char(4)););
create table AAA_PROD_XEUSS.P_F_OPERATION (b int, OPERATION int);
create table BBB_PROD_XEUSS.P_E_LVR_CQT (c int, OPERATION int, PROCESS char(4));
create table BBB_PROD_XEUSS.P_F_OPERATION (b int, OPERATION int);
此选择
select *
from AAA_PROD_XEUSS.P_E_LVR_CQT CQT
left join AAA_PROD_XEUSS.P_F_OPERATION OPER
USING ( OPERATION )
union
select *
from BBB_PROD_XEUSS.P_E_LVR_CQT CQT
left join BBB_PROD_XEUSS.P_F_OPERATION OPER
USING ( OPERATION )
where CQT.PROCESS = '1111'
Order by OPERATION
只返回一个名为OPERATION的列
OPERATION I PROCESS B
--------- - ------- -
0 record(s) selected.
P.S。您可能更愿意使用UNION ALL
而不仅仅是UNION
来避免Db2必须对所有行进行排序以删除重复项