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) 提供有关所提供的各种活动类型的管理信息的查询。对于每种类型的活动,查询应显示参加该类型活动的总人数以及参加每种活动的平均人数。

不幸的是,我收到以下错误:

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);
4

3 回答 3

1

问题出在AVG(etva).

您不能在定义别名的地方使用别名。SELECT list

更大的问题是您不能 AVG对值 ( SUM()) 使用聚合函数。

根据您需要的任务描述,AVG(etv)因为它已经在上面由codingbiz指出。

继续 发布新查询后。

我的理解是,EVENTS会有记录将成员与他们参与的活动联系起来。

JOIN EVENTS如果是这样,您将需要sub-query计算来自EVENTS“活动”和“活动”的成员的计数,以获取活动标题。

于 2013-03-07T22:23:39.847 回答
0

正如我从发布的结构中了解的那样。每个活动都是独一无二的,在每个活动中只参加一个成员。因此,您无法计算每个活动的平均成员数,因为它只是具有特定活动的事件的总和。只有参与者总数是可能的:

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

如果我错了,请纠正我。

于 2013-03-07T23:10:48.160 回答
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
  /
于 2013-03-08T14:08:17.623 回答