[英]Single-group group function error
I'm new to SQL and am currently having an error as stated in the title for my sql. 我是SQL新手,目前遇到错误,如我的sql标题中所述。
This is my SQL query: 这是我的SQL查询:
SELECT APPLICANT.APPNUM,APPLICANT.APPNAME,COUNT(APPLICATION.APPCNAPPNUM) AS "Total No. of Positions Applied"
FROM APPLICANT,APPLICATION
WHERE APPLICANT.APPNUM = APPLICATION.APPCNAPPNUM;
This is the Create statements: 这是Create语句:
CREATE TABLE APPLICANT (
appNum varchar2(10) not null,
appName varchar2(70),
appDOB date,
appCity varchar2(70),
appStreet varchar2(70),
appHouseNum varchar2(10),
constraint applicant_Pkey primary key (appNum)
);
CREATE TABLE APPLICATION (
appcnPosNum varchar2(10) not null,
appcnPosStOffrDt date not null,
appcnAppNum varchar2(10) not null,
appcnDt date,
constraint application_Pkey primary key (appcnPosNum, appcnPosStOffrDt, appcnAppNum),
constraint application_Fkey1 foreign key (appcnPosNum, appcnPosStOffrDt) references
POSITION(posNum, posStartOfferDt),
constraint application_Fkey2 foreign key (appcnAppNum) references APPLICANT(appNum)
);
Can someone give me any suggestions as how to solve this error? 有人可以给我任何有关解决此错误的建议吗?
Use GROUP BY
like: 使用GROUP BY
例如:
SELECT APPLICANT.APPNUM,APPLICANT.APPNAME,COUNT(APPLICATION.APPCNAPPNUM) AS "Total No. of Positions Applied"
FROM APPLICANT,APPLICATION
WHERE APPLICANT.APPNUM = APPLICATION.APPCNAPPNUM
GROUP BY APPLICANT.APPNUM,APPLICANT.APPNAME
It's good to use also INNER JOIN
instead of old school multiple tables, like: 最好也使用INNER JOIN
而不是老式的多个表,例如:
SELECT APPLICANT.APPNUM,APPLICANT.APPNAME,COUNT(APPLICATION.APPCNAPPNUM) AS "Total No. of Positions Applied"
FROM APPLICANT
INNER JOIN APPLICATION
ON APPLICANT.APPNUM = APPLICATION.APPCNAPPNUM
GROUP BY APPLICANT.APPNUM,APPLICANT.APPNAME
Whenever you have an aggregate function (like COUNT) you need to include a GROUP BY clause in your query that contains all the non-aggregate fields from the "SELECT" projection. 只要有聚合函数(如COUNT),就需要在查询中包括GROUP BY子句,该子句包含“ SELECT”投影中的所有非聚合字段。 For this specific case, you need to do the following: 对于这种特定情况,您需要执行以下操作:
SELECT APPLICANT.APPNUM,APPLICANT.APPNAME,COUNT(APPLICATION.APPCNAPPNUM) AS "Total No. of Positions Applied"
FROM APPLICANT,APPLICATION
WHERE APPLICANT.APPNUM = APPLICATION.APPCNAPPNUM
GROUP BY APPLICANT.APPNUM, APPLICANT.APPNAME;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.