我有两张桌子 - 员工和经理。一位经理有很多员工。我想使用CONNECT BY和GROUP BY命令在父子关系中显示它。
EMPLOYEE表的结构:
CREATE TABLE employee (
employee_id INTEGER,
manager_id INTEGER,
first_name VARCHAR2(10) NOT NULL,
last_name VARCHAR2(10) NOT NULL
);
经理表的结构:
CREATE TABLE manager(
manager_id INTEGER NOT NULL,
manager_dept VARCHAR2(20) NOT NULL,
first_name VARCHAR2(30) NOT NULL,
last_name VARCHAR2(30) NOT NULL
);
我写了这个查询:
SELECT E.EMPLOYEE_ID, M.MANAGER_ID, E.FIRST_NAME, E.LAST_NAME, CONNECT_BY_ISLEAF, CONNECT_BY_ISCYCLE
FROM EMPLOYEE E
LEFT OUTER JOIN MANAGER M
ON E.MANAGER_ID=M.MANAGER_ID
connect by NOCYCLE m.manager_id=prior e.employee_id
GROUP BY manager_id;
它给出了这个错误:
ORA-00918: column ambiguously defined
00918. 00000 - "column ambiguously defined"
Error at Line: 6 Column: 10
这个group by子句有什么问题?
答案 0 :(得分:0)
您将获得column ambiguously defined
,因为oracle无法确定哪个表的manager_id应该用于组,因为您尚未指定它。正确的查询是:
SELECT M.MANAGER_ID
FROM EMPLOYEE E
LEFT OUTER JOIN MANAGER M
ON E.MANAGER_ID=M.MANAGER_ID
connect by NOCYCLE m.manager_id=prior e.employee_id
GROUP BY m.manager_id;
答案 1 :(得分:0)
GROUP BY中的列manager_id是不明确的 - 它可以是employee表的manager_id,也可以是manager表的manager_id。
您还需要将剩余的列添加到GROUP BY。然后完整的查询变为:
SELECT
E.EMPLOYEE_ID, M.MANAGER_ID, E.FIRST_NAME,
E.LAST_NAME, CONNECT_BY_ISLEAF, CONNECT_BY_ISCYCLE
FROM EMPLOYEE E
LEFT OUTER JOIN MANAGER M
ON E.MANAGER_ID=M.MANAGER_ID
connect by NOCYCLE m.manager_id=prior e.employee_id
GROUP BY
E.EMPLOYEE_ID, M.MANAGER_ID, E.FIRST_NAME,
E.LAST_NAME, CONNECT_BY_ISLEAF, CONNECT_BY_ISCYCLE;