You could use what your query returns to get the extra row that have CONNECT_BY_ISCYCLE=1
WITH a AS
(
SELECT EMP_ID, FIRST_NAME, MANAGER_EMP_ID, CONNECT_BY_ISCYCLE
FROM MYTABLE
CONNECT BY NOCYCLE
PRIOR MANAGER_EMP_ID = EMP_ID
START WITH EMP_ID = '12345'
)
SELECT EMP_ID, FIRST_NAME
FROM a
UNION ALL
SELECT m.EMP_ID, m.FIRST_NAME
FROM MYTABLE m
JOIN a
ON a.MANAGER_EMP_ID = m.EMP_ID
WHERE a.CONNECT_BY_ISCYCLE = 1 ;
Or use a recursive CTE:
WITH a (EMP_ID, FIRST_NAME, MANAGER_EMP_ID) AS
(
SELECT EMP_ID, FIRST_NAME, MANAGER_EMP_ID
FROM MYTABLE
WHERE EMP_ID = '12345'
UNION ALL
SELECT m.EMP_ID, m.FIRST_NAME, m.MANAGER_EMP_ID
FROM MYTABLE m
JOIN a
ON a.MANAGER_EMP_ID = m.EMP_ID
AND a.MANAGER_EMP_ID <> a.EMP_ID -- to avoid the cycles
)
SELECT EMP_ID, FIRST_NAME
FROM a ;
Another way to avoid cycles in a recursive CTE is to use the CYCLE
clause (available only in Oracle):
WITH a (EMP_ID, FIRST_NAME, MANAGER_EMP_ID) AS
(
SELECT EMP_ID, FIRST_NAME, MANAGER_EMP_ID
FROM MYTABLE
WHERE EMP_ID = 1
UNION ALL
SELECT m.EMP_ID, m.FIRST_NAME, m.MANAGER_EMP_ID
FROM MYTABLE m
JOIN a
ON a.MANAGER_EMP_ID = m.EMP_ID
)
CYCLE MANAGER_EMP_ID
SET IS_CYCLE TO 'Y' DEFAULT 'N'
SELECT EMP_ID, FIRST_NAME, IS_CYCLE
FROM a ;
Test at SQLfiddle
Just realized that the extra row can be retrieved with a simple change in the original query:
SELECT EMP_ID, FIRST_NAME, MANAGER_EMP_ID, CONNECT_BY_ISCYCLE
FROM MYTABLE
CONNECT BY NOCYCLE
PRIOR MANAGER_EMP_ID = EMP_ID
AND PRIOR MANAGER_EMP_ID <> PRIOR EMP_ID
-- if she's a manager
-- don't try to connect her with superiors
START WITH EMP_ID = '12345' ;
ORA-00979 not a GROUP BY expression
Cause: The GROUP BY clause does not contain all the expressions in the SELECT clause. SELECT expressions that are not included in a group function, such as AVG, COUNT, MAX, MIN, SUM, STDDEV, or VARIANCE, must be listed in the GROUP BY clause.
Action: Include in the GROUP BY clause all SELECT expressions that are not group function arguments.
Gt.description
column is not listed in the group by
clause. Columns not participating in group function should be listed in group by clause.
SELECT gt.grade_type_code,
gt.description,
round(avg(gg.numeric_grade),2) AS "Average Grade"
FROM student.grade_type gt
JOIN student.grade gg ON gt.grade_type_code = gg.GRADE_TYPE_CODE
GROUP BY gt.grade_type_code,
gt.description
ORDER BY gt.grade_type_code;
Best Answer
You could try this:
[edit] fixed a bug .. comment on line fixed [/edit]