我有以下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)提供有关占用的管理信息的查询 提供各种类型的活动。对于每种类型的活动, 查询应显示采用该类型的个人总数 活动和每种类型的平均人数 活性。
不幸的是我收到以下错误:
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);
答案 0 :(得分:1)
问题在于AVG(etva)
。
您不能在定义它的同一SELECT list
中使用别名。
更大的问题是您不能在值(AVG
)上使用聚合函数SUM()
。
根据任务说明,您需要AVG(etv)
,因为 codingbiz 已在上面指出。
CONTINUE 发布新查询后。
我的理解是EVENTS
将记录成员与他们参与的活动。
如果是这样,您需要JOIN
EVENTS
与sub-query
一起计算来自EVENTS
和'ACTIVITIES'的成员的计数才能获得活动标题。
答案 1 :(得分:0)
我从发布的结构中了解到。每个活动都是独一无二的,每个活动只参加一个会员。因此,您不能计算每个活动的平均会员数,因为它只是具有特定活动的事件的总和。只有参与者总数是可能的:
SELECT ACTIVITY_TYPE, COUNT(EVENT_ID)
FROM ACTIVITIES, "EVENTS"
WHERE ACTIVITIES.ACTIVITY_ID="EVENTS".ACTIVITY_ID
GROUP BY ACTIVITY_TYPE;
如果我错了,请纠正我。
答案 2 :(得分: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
/