我有多个查询正在尝试合并为一个没有任何运气的查询。我在同一个表上两次使用左联接,但字段不同,这听起来是错误的。
SELECT a.*
, b.CODE_DESCRIPTION AS highest_grade
FROM BBOP.EP_MAIN_FACT a
LEFT JOIN BBOP.EP_CODE_WORK b
ON a.HIGHESTGRADE_CA = b.code
AND code_type LIKE 'High%'
LEFT JOIN BBOP.EP_CODE_WORK ab
ON a.Goal_Steps = ab.code
AND code_type LIKE 'Goal%'
WHERE plan_date BETWEEN '01-mar-2019' AND '31-may-2019';
-ORA-00918:列定义不明确 00918. 00000-“列定义不明确”
这是两个查询,它们分别产生结果,没有问题。
-最高成绩
SELECT a.*
, b.CODE_DESCRIPTION AS highest_grade_desc
FROM BBOP.EP_MAIN_FACT a
LEFT JOIN BBOP.EP_CODE_WORK b
ON a.HIGHESTGRADE_CA = b.code
WHERE plan_date BETWEEN '01-mar-2019' AND '31-may-2019'
AND code_type LIKE 'High%';
-目标
SELECT a.*
, b.CODE_DESCRIPTION AS Goal
FROM BBOP.EP_MAIN_FACT a
LEFT JOIN BBOP.EP_CODE_WORK b
ON a.Goal_Steps = b.code
WHERE plan_date BETWEEN '01-mar-2019' AND '31-may-2019'
AND code_type LIKE 'Goal%';
答案 0 :(得分:0)
我想你想要
select mf.*, coalesce(cwh.CODE_DESCRIPTION, cwg.CODE_DESCRIPTION) as highest_grade
from BBOP.EP_MAIN_FACT mf left join
BBOP.EP_CODE_WORK cwh
on mf.HIGHESTGRADE_CA = cwh.code and
cwh.code_type like 'High%' left join
BBOP.EP_CODE_WORK cwg
on mf.Goal_Steps = cwg.code and
cwg.code_type like 'Goal%'
where mf.plan_date >= date '2019-03-01' and
mf.plan_date < date '2019-06-01';
注意:
on
子句中有“裸”列引用。coalesce()
根据联接的优先级顺序选择值。date
关键字来引入日期文字。这比依赖可能在给定服务器上更改的默认格式更安全。between
对于日期很危险,尤其是在Oracle中date
类型始终具有时间成分。不等式抓住了逻辑。答案 1 :(得分:0)
正如有人已经在评论中指出的那样,plan_date和code_type列需要在其用法中指定表别名。
我猜这些列中至少有一列在BBOP.EP_CODE_WORK表中,因此在引用这些列时需要指定表别名。
问候 阿卡什(Akash)