2017-03-03 67 views
-1

我試圖獲得一個項目在SQL中的工作量,但由部分查詢組有一些錯誤。我有一個單獨的表(workersonprojects),它與工作項目ID相匹配。查詢應該選擇所有內容+一個項目上的工作人員數量。爲什麼我會收到錯誤「不是GROUP BY表達式」?

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; 
+0

因爲您只能使用select中的列,它們在Group by Statement或聚合函數 – Jens

+0

projectid中是什麼?你需要告訴GROUP BY。 projects.projectid – hurricane

+1

一般的GROUP BY規則說:如果指定了GROUP BY子句,SELECT列表中的每個列引用必須標識一個分組列或者是一個set函數的參數! – jarlh

回答

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; 
0

您在使用聚合函數:COUNT()。您必須按COUNT()以外的所有其他列進行分組。

0

根據其他答案:

SELECT專案編號,名稱,developerleader,consultantleader,projectleader,預算,計數(workerid)工人 從上 JOIN workersonprojects項目(專案編號=項目) 通過查詢JOIN上(工人= workerid) GROUP BY專案編號,名稱,developerleader,consultantleader,projectleader,預算

0

工人組你必須定義 '專案編號',這表喲你想。

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; 
0

基本上對於Group BY子句,您需要在Group by子句中包含所有不在任何Aggregate Function中使用的列。在這種情況下,您有SELECTED投影,名稱,developerleader,consultantleader,projectleader,預算列,但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; 
0

您必須與所有給定的select語句屬性進行分組,因此請給出您在select語句中選擇的所有屬性。

相關問題