我有以下SQL代碼:聚合函數的別名錯誤
SELECT ACTIVITY_TYPE, COUNT(EVENT_ID) as etv, SUM(etv) as etva, AVG(etva)
FROM ACTIVITIES, "EVENTS"
WHERE ACTIVITIES.ACTIVITY_ID="EVENTS".ACTIVITY_ID
GROUP BY ACTIVITY_TYPE;
這是一個試圖回答這樣一個問題:
d) A query that provides management information on take up of the various types of activities on offer. For each type of activity, the query should show the total number of individuals who took that type of activity and the average number of individuals taking each type of activity.
不幸的是我得到以下錯誤:
ORA-00904: "ETVA": invalid identifier
任何幫助將是偉大的!
編輯:
SELECT ACTIVITY_TYPE, COUNT(EVENT_ID), AVG(COUNT(EVENT_ID))
FROM ACTIVITIES, "EVENTS"
WHERE ACTIVITIES.ACTIVITY_ID="EVENTS".ACTIVITY_ID
GROUP BY ACTIVITY_TYPE;
這是新的查詢現在我收到此錯誤:
ORA-00937: not a single-group group function
表結構:
CREATE TABLE "GROUPS" (
GROUP_ID NUMBER(4) NOT NULL CONSTRAINT GROUP_ID_PK PRIMARY KEY ,
GROUP_NAME VARCHAR2(35) NULL,
GROUP_NUMBER_OF_MEMBERS NUMBER(3) NOT NULL ,
GROUP_LEADER_ID NUMBER(4) NOT NULL
);
CREATE TABLE MEMBERS (
MEMBER_ID NUMBER(4) NOT NULL CONSTRAINT MEMBER_ID_PK PRIMARY KEY ,
MEMBER_SURNAME VARCHAR2(35) NOT NULL ,
MEMBER_FIRST_NAME VARCHAR2(35) NOT NULL ,
MEMBER_DOB DATE NOT NULL ,
MEMBER_GENDER VARCHAR2(1) NOT NULL CONSTRAINT MEMBER_GENDER_CHECK CHECK (MEMBER_GENDER IN ('M' , 'F')),
GROUP_ID NUMBER(4) NOT NULL,
CONSTRAINT GROUP_ID_FK_MEMBER FOREIGN KEY (GROUP_ID) REFERENCES "GROUPS"(GROUP_ID)
);
CREATE TABLE CHALETS (
CHALET_NO NUMBER(4) NOT NULL CONSTRAINT CHALET_ID_PK PRIMARY KEY ,
CHALET_NAME VARCHAR2(35) NOT NULL,
CHALET_CAPACITY NUMBER(2) NOT NULL,
CHALET_RATE NUMBER(6 , 2) NOT NULL
);
CREATE TABLE SUPERVISORS (
SUPERVISOR_ID NUMBER (4) NOT NULL CONSTRAINT SUPERVISOR_ID_PK PRIMARY KEY ,
SUPERVISOR_SURNAME VARCHAR2(35) NOT NULL ,
SUPERVISOR_FIRST_NAME VARCHAR2(35) NOT NULL ,
SUPERVISOR_MOBILE_NUMBER VARCHAR2(11) NOT NULL
);
CREATE TABLE ACTIVITIES (
ACTIVITY_ID NUMBER (4) NOT NULL CONSTRAINT ACTIVITY_ID_PK PRIMARY KEY ,
ACTIVITY_TYPE VARCHAR (35) NOT NULL ,
ACTIVITY_TITLE VARCHAR2(35) NOT NULL
);
CREATE TABLE BOOKINGS (
BOOKING_ID NUMBER(4) NOT NULL CONSTRAINT BOOKING_ID_PK PRIMARY KEY ,
GROUP_ID NUMBER(4) NOT NULL ,
CHALET_ID NUMBER(4) NOT NULL ,
BOOKING_START_DATE DATE NOT NULL ,
BOOKING_END_DATE DATE NOT NULL ,
CONSTRAINT GROUP_ID_FK_BOOKING FOREIGN KEY (GROUP_ID) REFERENCES GROUPS(GROUP_ID) ,
CONSTRAINT CHALET_ID_FK_BOOKING FOREIGN KEY (CHALET_ID) REFERENCES CHALETS(CHALET_NO)
);
CREATE TABLE "EVENTS" (
EVENT_ID NUMBER (4) NOT NULL CONSTRAINT EVENT_ID_PK PRIMARY KEY ,
MEMBER_ID NUMBER (4) NOT NULL ,
SUPERVISOR_ID NUMBER (4) NOT NULL ,
ACTIVITY_ID NUMBER (4) NOT NULL ,
EVENT_COMMENTS VARCHAR2(500) ,
EVENT_DATE DATE NOT NULL ,
EVENT_TIME_SLOT VARCHAR2(2) NOT NULL ,
CONSTRAINT MEMBER_ID_FK_EVENT FOREIGN KEY (MEMBER_ID) REFERENCES MEMBERS(MEMBER_ID) ,
CONSTRAINT SUPERVISOR_ID_FK_EVENT FOREIGN KEY (SUPERVISOR_ID) REFERENCES SUPERVISORS(SUPERVISOR_ID) ,
CONSTRAINT ACTIVITY_ID_FK_EVENT FOREIGN KEY (ACTIVITY_ID) REFERENCES ACTIVITIES(ACTIVITY_ID)
);
ALTER TABLE "GROUPS" ADD CONSTRAINT MEMBER_ID_FK_GROUP_LEADER FOREIGN KEY (GROUP_LEADER_ID) REFERENCES MEMBERS(MEMBER_ID);
您的意思是'AVG(ETV)'代替AVG(etva)'? – codingbiz 2013-03-07 22:20:44