我有两个表,由这个 create 语句定义
CREATE TABLE PROJECT (project_name varchar(255), project_budget int, dedicated_project_leader int, dedicated_lead_developer int, dedicated_lead_consultant int, company_id int, project_id int, PRIMARY KEY (project_id));
CREATE TABLE WORKER (worker_name varchar(255), worker_salary int, worker_anual_bonus int, worker_type varchar(255), leader int, company_id int, project_id int, worker_id int, PRIMARY KEY (worker_id));
我想问的是项目预算的总和,而不是除以工人的数量---)
SELECT (SUM(p.project_budget) / (SELECT count(WORKER.WORKER_NAME) from WORKER)) as AVG_BONUS
FROM PROJECT p;
不幸的是,这引发了“ORA--00937:not a single-group group function”异常。
但是,如果我执行此查询:
SELECT count(WORKER.WORKER_NAME) from WORKER)
结果将是:
16
如果我将预算总和除以数字而不是结果:
SELECT (SUM(p.project_budget) / 16) as AVG_BONUS
FROM PROJECT p;
我也得到了一个完美的结果,例如:
294117,647058823
解决方案是我需要按项目表中定义的一些 id 进行分组,但不认为这是合乎逻辑的......
SELECT (SUM(p.project_budget) / (SELECT count(WORKER.WORKER_NAME) from WORKER)) as AVG_BONUS
FROM PROJECT p
GROUP BY p.company_id;
它也在工作。
有没有人建议我为什么不能使用它?(我感觉这只是一个 sql 语言规范,在没有 group by 的情况下不能在 select 中存在两个 group 函数......)