我的查询有问题,请帮我解决这个问题。
我的查询:
SELECT D.DEPTNO,
(SELECT COUNT(DISTINCT P.PROJNO) FROM SCHEMA.PROJECT P, SCHEMA.DEPARTMENT D WHERE P.DEPTNO = D.DEPTNO) AS PROJECT,
(SELECT COUNT(DISTINCT E.EMPNO) FROM SCHEMA.EMPLOYEE E, SCHEMA.DEPARTMENT D WHERE E.WORKDEPT = D.DEPTNO) AS EMPLOYEE
FROM SCHEMA.DEPARTMENT D, SCHEMA.PROJECT P, SCEHMA.EMPLOYEE E GROUP BY D.DEPTNO@
以及结果: enter image description here
但每行应该有不同的结果。
我必须显示每个部门的项目和员工总数,所以我通过deptno对其进行分组,但结果显示了所有项目和员工
请帮助我们:)
答案 0 :(得分:0)
当你加入表时 - 不要忘记连接谓词! 如果我的要求正确 - 这可能是一个可能的解决方案:
select deptno
,count_projects
,count_employees
from (
select deptno
,count(projno) as count_projects
from project
group by deptno ) as p
inner join
(select workdept
,count(*) as Count_employees
from employee
group by workdept) as e
on p.deptno = e.workdept
除非您必须从此表中检索特定数据,否则不需要表DEPARTMENT。
答案 1 :(得分:0)
我认为您正在尝试编写此查询:
SELECT D.DEPTNO,
(SELECT COUNT(DISTINCT P.PROJNO)
FROM SCHEMA.PROJECT P
WHERE P.DEPTNO = D.DEPTNO
) AS PROJECT,
(SELECT COUNT(DISTINCT E.EMPNO)
FROM SCHEMA.EMPLOYEE E
WHERE E.WORKDEPT = D.DEPTNO
) AS EMPLOYEE
FROM SCHEMA.DEPARTMENT D;
注意:
JOIN
个。相关条款就足够了。GROUP BY
。COUNT(DISTINCT)
,但我不确定,所以我要离开它。英寸FROM
子句中使用逗号。 始终使用明确的JOIN
语法。