我为什么会收到错误"不是GROUP BY表达式"?

时间:2017-03-03 10:18:15

标签: sql oracle oracle11g

我试图在sql中获取项目的工作量,但是该部分查询的组有一些错误。我有一个separete表(workersonprojects),它与项目id-s和正在处理它的worker相匹配。查询应该选择所有项目+ 1个项目中的工人数量。

SELECT projectid, name, developerleader, consultantleader, projectleader, budget, count(workerid) AS workers
FROM projects
JOIN workersonprojects on (projectid=project)
JOIN workers on (worker=workerid)
GROUP BY projectid;

6 个答案:

答案 0 :(得分:2)

这是因为从查询group by可以看出,您没有将选择列表中的所有列都包含在GROUP BY projectid;子句中。您应该更改您的查询,如

SELECT p.projectid, 
name, 
developerleader, 
consultantleader, 
projectleader, 
budget,
xxx.workers
FROM projects p
JOIN workersonprojects wp on p.projectid= wp.project
JOIN workers w on p.worker= w.workerid
JOIN (select projectid, count(workerid) AS workers
FROM projects
GROUP BY projectid ) xxx ON p.projectid = xxx.projectid;

答案 1 :(得分:0)

您正在使用聚合函数:COUNT()。您必须按除COUNT()之外的所有其他列进行分组。

答案 2 :(得分:0)

根据其他答案:

SELECT projectid,name,developerleader,consultantleader,projectleader,budget,count(workerid)AS worker 来自项目 JOIN workersonprojects on(projectid = project) 加入工人(工人=工人) GROUP BY projectid,name,developerleader,consultantleader,projectleader,budget

答案 3 :(得分:0)

在逐个查询中,您必须定义' projectid'你想要哪张桌子。

SELECT workersonprojects.projectid, projects.name, 
projects.developerleader, projects.consultantleader, projects.projectleader,
projects.budget, 
count(workersonprojects.workerid) AS workers
FROM projects
JOIN workersonprojects on 
(workersonprojects.projectid=projects.projectid)
JOIN workers on (workers.worker=workersonprojects.workerid)
GROUP BY workersonprojects.projectid;

答案 4 :(得分:0)

基本上对于Group BY子句,您需要在Group by子句中包含所有未在任何聚合函数中使用的列。在这种情况下,您有SELECTED projectid,name,developerleader,consultantleader,projectleader,budget列,但我们在COUNT()函数中只有一列。因此,请在SELECT子句中包含这些列,并使您有一个有效的查询。

SELECT projectid,
  name,
  developerleader,
  consultantleader,
  projectleader,
  budget,
  COUNT(workerid) AS workers
FROM projects
JOIN workersonprojects
ON (projectid=project)
JOIN workers
ON (worker=workerid)
GROUP BY PROJECTID,
  name,
  developerleader,
  consultantleader,
  projectleader,
  budget;

答案 5 :(得分:0)

您必须使用所有给定的select语句属性进行分组,因此请在select语句中提供您选择的所有属性。