我有这个查询(运行得很好):
WITH TEMPTABLE AS(
SELECT count(CONTRACT_ID) AS no_contract, T.BRANCH_ID as branch
FROM CONTRACT, "EMPLOYMENT" T, TABLE(T."EMPLOYEE") TT
WHERE CONTRACT.EMPLOYEE_ID = TT.EMPLOYEE_ID
AND CONTRACT.CONTRACT_VT_START BETWEEN '01/01/2018' AND '30/06/2018'
GROUP BY T.BRANCH_ID
)
SELECT branch
FROM TEMPTABLE
WHERE no_contract IN (SELECT MAX(no_contract)
FROM TEMPTABLE);
我想知道是否可以用其他方式代替WITH子句来完成。像这样:
SELECT count_per_branch.BRANCH
FROM (SELECT count(CONTRACT_ID) AS no_contract, BRANCH_ID AS BRANCH
FROM CONTRACT, "EMPLOYMENT" T, TABLE(T."EMPLOYEE") TT
WHERE CONTRACT.EMPLOYEE_ID = TT.EMPLOYEE_ID
AND CONTRACT.CONTRACT_VT_START BETWEEN '01/01/2018' AND '30/06/2018'
GROUP BY T.BRANCH_ID) count_per_branch
WHERE count_per_branch.no_contract =
(SELECT max(count_per_branch.no_contract)
FROM count_per_branch);
哪个给出了ORA-00942:“表或视图不存在” 第9行错误:
答案 0 :(得分:2)
您可以在FROM子句中使用子查询:
FROM (SELECT count(CONTRACT_ID) ... )
您可以给它起一个别名:
FROM (SELECT count(CONTRACT_ID) ... ) count_per_branch
您可以使用该别名来限定列引用:
WHERE count_per_branch.no_contract =
但是您不能使用该别名作为另一个子查询中的行源,这是您要在此处执行的操作:
(SELECT max(count_per_branch.no_contract)
FROM count_per_branch)
要以这种方式“重用”子查询,必须使用通用表表达式语法-即WITH子句。