2013-03-07 90 views
0

我有以下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); 
+1

您的意思是'AVG(ETV)'代替AVG(etva)'? – codingbiz 2013-03-07 22:20:44

回答

0

查詢中的etv和etva不是列,而是別名。你不能選擇它們。 要選擇他們按照這個和您的查詢/列給出有意義的別名:

SELECT ACTIVITY_TYPE, count_etv, sum_etva, AVG(sum_etva) avg_atva 
    FROM 
    (
    SELECT ACTIVITY_TYPE, count_etv, SUM(count_etv) as sum_etva 
    FROM 
    (
    SELECT ACTIVITY_TYPE, COUNT(EVENT_ID) as count_etv 
     --, SUM(etv) as etva, AVG(etva) -- NOT columns you cannot select them 
    FROM ACTIVITIES, "EVENTS" 
    WHERE ACTIVITIES.ACTIVITY_ID="EVENTS".ACTIVITY_ID 
    GROUP BY ACTIVITY_TYPE 
    ) 
    GROUP BY ACTIVITY_TYPE, count_etv 
) 
GROUP BY ACTIVITY_TYPE, count_etv, sum_etva 
/

第二次查詢 - 清潔,更具可讀性...:

SELECT ACTIVITY_TYPE -- optional 
     , total_events -- optional 
    , AVG(total_events) avg_events 
    FROM 
    (
    SELECT ACTIVITY_TYPE 
     , COUNT(EVENT_ID) total_events 
    FROM ACTIVITIES, "EVENTS" 
    WHERE ACTIVITIES.ACTIVITY_ID="EVENTS".ACTIVITY_ID 
    GROUP BY ACTIVITY_TYPE 
    ) 
    GROUP BY ACTIVITY_TYPE, total_events 
/
1

問題是與AVG(etva)

在定義它的相同SELECT list中不能使用別名

更大的問題是,您不能在值(SUM())上使用聚合函數AVG

根據任務描述,您需要AVG(etv),因爲它已經在上面通過編碼商標指出。

繼續發佈新查詢後。

我的理解是,EVENTS將連接件與他們參加活動的記錄。

如果是這樣,你需要JOINEVENTSsub-query計算計數成員EVENTS與「活動」先手活動標題。

+0

謝謝請參閱我的更新:) – 2013-03-07 22:38:54

+0

@David Passmore - 完全相同的答案:不能在值('COUNT()')上使用聚合函數('AVG')。 – 2013-03-07 22:42:50

+0

@ PM_77-1好的,你會怎麼建議我去做這件事? – 2013-03-07 22:44:42

0

據我瞭解張貼結構。每個活動都是唯一的,每個活動只參加一個會員。因此,您無法統計每個活動的平均會員數量,因爲這只是活動與特定活動的總和。只有參與者的總人數是可能的:

SELECT ACTIVITY_TYPE, COUNT(EVENT_ID) 
FROM ACTIVITIES, "EVENTS" 
WHERE ACTIVITIES.ACTIVITY_ID="EVENTS".ACTIVITY_ID 
GROUP BY ACTIVITY_TYPE; 

糾正我,如果我錯了。

+0

不完全,一個成員可以參加更多的一個活動,並且不止一個成員可以參與一個活動。事件表是爲了防止多對多的關係。 – 2013-03-07 23:13:13

+0

你說:「所以你不能計算每個活動會員的平均數量,因爲只是活動與特定活動的總和」,但是如果你總結了COUNT的AVG,那麼它會起作用,不是嗎? – 2013-03-07 23:17:46

+0

嗯。假設我們的查詢結果(Activity1,15),(Activity2,4)。你如何計算每個活動的平均值? – www 2013-03-07 23:21:08