I have a syntax error on an Oracle SQL query. The query should get me the department of an employee. If the employee has no department (null), I want the department of the first manager up the hierarchy, which has a department.
SELECT department_id FROM department
WHERE department_id =
(
SELECT department_id FROM employee
WHERE department_id IS NOT NULL AND rownum = 1
start WITH employee_id = 19
connect by employee_id = prior manager_id
ORDER BY level
);
The error message is this:
ORA-00907: missing right parenthesis
00907. 00000 - "missing right parenthesis"
*Cause:
*Action:
Error at Line: 8 Column: 2
I have written an alternative query, which does the job. But I'm not very happy with it.
SELECT department_id FROM department
WHERE department_id =
(
SELECT department_id FROM employee
WHERE level =
(
SELECT MIN(level) FROM employee
WHERE department_id IS NOT NULL
start WITH employee_id = 19
connect by employee_id = prior manager_id
)
start WITH employee_id = 19
connect by employee_id = prior manager_id
);
Do you have any idea how to fix the first query? Or to simplify the second one? Thank you in advance.
In your first query as correctly said by @Barbaros that
ORDER BYis not needed and evenrownum = 1will not do what you are thinking.Does the following query fulfill your requirements:
Cheers!!