下面的查詢按預期執行,給我定的字段被查詢爲什麼我的查詢在添加SUM函數後失敗?
SELECT 1 as SEQ, TERM_DESC as TERM, PRIMARY_COLLEGE_DESC as COLLEGE, LEVEL_GROUPING_CODE as LEVEL_CODE, LEVEL_GROUPING_DESC as LEVEL_DESC
FROM SECopy as SE
然而預期的輸出,當我添加函數SUM()在現場,我收到有關TERM_DESC以下錯誤,它甚至與使用SUM()函數的ID_COUNT沒有關係。
SELECT 1 as SEQ, TERM_DESC as TERM, PRIMARY_COLLEGE_DESC as COLLEGE, LEVEL_GROUPING_CODE as LEVEL_CODE, LEVEL_GROUPING_DESC as LEVEL_DESC, SUM(ID_COUNT) as HEADCOUNT
from SECopy as se
,我發現了以下錯誤:
Column 'Student_Enrollment_copy.TERM_DESC' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
爲什麼會加入SUM()函數後才失敗?
任何不在聚合中的字段(如'sum()')必須位於「GROUP BY」中 – 2014-11-14 18:06:25
我不確定,但我認爲這是因爲沒有分組。我認爲添加一個聚合函數會迫使開發者添加一個group by子句。你可以嘗試在田野上做一個小組嗎? – Rika 2014-11-14 18:08:14
AFAIK SQL Server *在使用聚合函數時需要* GROUP BY。有RDBMS(如MySQL)跳過這一要求 – Barranka 2014-11-14 18:13:53