单组组功能错误

时间:2013-10-30 13:39:03

标签: sql

我是SQL新手,目前我的sql标题中有错误。

这是我的SQL查询:

SELECT APPLICANT.APPNUM,APPLICANT.APPNAME,COUNT(APPLICATION.APPCNAPPNUM) AS "Total No. of Positions Applied"
FROM APPLICANT,APPLICATION
WHERE APPLICANT.APPNUM = APPLICATION.APPCNAPPNUM;

这是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)
);

有人可以就如何解决此错误给我任何建议吗?

2 个答案:

答案 0 :(得分:3)

使用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

最好还使用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

答案 1 :(得分:1)

每当你有一个聚合函数(比如COUNT)时,你需要在查询中包含一个GROUP BY子句,它包含“SELECT”投影中的所有非聚合字段。对于这种特定情况,您需要执行以下操作:

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;