0

我在 SQL 中有以下问题要解决:

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

这是我的桌子:

CREATE TABLE accommodations
(
chalet_number int PRIMARY KEY,
chalet_name varchar(40) NOT NULL,
no_it_sleeps number(2) NOT NULL,
indivppw number(4) NOT NULL
)


CREATE TABLE supervisors
(
supervisor_number int PRIMARY KEY,
supervisor_forename varchar(30) NOT NULL,
supervisor_surname varchar(30) NOT NULL,
mobile_number varchar(11) NOT NULL
)



CREATE TABLE visitors
(
visitor_ID int PRIMARY KEY,
group_ID int NOT NULL,
forename varchar(20) NOT NULL,
surname varchar(20) NOT NULL,
dob date NOT NULL,
gender varchar(1) NOT NULL
)


CREATE TABLE activities
(
activity_code varchar(10) PRIMARY KEY,
activity_title varchar(20) NOT NULL,
"type" varchar(20) NOT NULL
)


CREATE TABLE "groups"
(
group_ID int PRIMARY KEY,
group_leader varchar(20) NOT NULL,
group_name varchar(30)
number_in_group number(2) NOT NULL
)


CREATE TABLE bookings
(
group_ID int NOT NULL,
start_date date NOT NULL,
chalet_number int NOT NULL,
no_in_chalet number(2) NOT NULL,
start_date date NOT NULL,
end_date date NOT NULL,
CONSTRAINT bookings_pk PRIMARY KEY(group_ID, chalet_number));


CREATE TABLE schedule
(
schedule_ID int PRIMARY KEY,
activity_code varchar(10) NOT NULL,
time_of_activity number(4,2) NOT NULL,
am_pm varchar(2) NOT NULL,
"date" date NOT NULL
)


CREATE TABLE activity_bookings
(
visitor_ID int NOT NULL,
schedule_ID int NOT NULL,
supervisor_number int NOT NULL,
comments varchar(200),
CONSTRAINT event_booking_pk PRIMARY KEY(visitor_ID, schedule_ID));





ALTER TABLE visitors
ADD FOREIGN KEY (group_ID)
REFERENCES "groups"(group_ID)

ALTER TABLE Schedule
ADD FOREIGN KEY (activity_code)
REFERENCES activities(activity_code)


ALTER TABLE bookings
ADD FOREIGN KEY (group_ID)
REFERENCES "groups"(group_ID)


ALTER TABLE bookings
ADD FOREIGN KEY (chalet_number)
REFERENCES accommodations(chalet_number)



ALTER TABLE activity_bookings
ADD FOREIGN KEY (visitor_ID)
REFERENCES visitors(visitor_ID)


ALTER TABLE activity_bookings
ADD FOREIGN KEY (schedule_ID)
REFERENCES schedule(schedule_ID)


ALTER TABLE activity_bookings
ADD FOREIGN KEY (supervisor_number)
REFERENCES supervisors(supervisor_number)

我有以下解决方案:

SELECT activities."type", 'overalltotal' AS OT, ('overalltotal' / 'activities') AS AVG
  FROM activities, schedule
  WHERE 'overalltotal' = (SELECT SUM(COUNT(schedule_ID))
        FROM activities, schedule
        WHERE schedule.activity_code = activities.activity_code
        GROUP BY activities."type"
        )
  AND 'activities' = (SELECT COUNT(DISTINCT activities."type")
        FROM activities
        )
  AND schedule.activity_code = activities.activity_code
GROUP BY activities."type";

我已经实现了示例数据和代码来检查上面的变量:

SELECT SUM(COUNT(schedule_ID))
        FROM activities, schedule
        WHERE schedule.activity_code = activities.activity_code
        GROUP BY activities."type";

结果:20

SELECT COUNT(DISTINCT activities."type")
FROM activities;

结果:5

但是在运行代码时:

ORA-01722: invalid number
01722. 00000 -  "invalid number"
*Cause:    
*Action:

编辑:

使用戴夫的代码我有以下输出:

Snowboarding    15
sledding            19
Snowmobiling    6
Ice Skating         5
Skiing          24

我将如何做问题的最后一部分?

以及参加每种活动的平均人数。

4

1 回答 1

1

您必须在 Oracle 中的列名周围使用双引号,而不是单引号。例如,"overalltotal"。单引号用于文本字符串,这就是您收到无效数字错误的原因。

编辑:这可能是您要使用的查询类型:

SELECT activities."type", COUNT(*) AS total, COUNT(*)/(COUNT(*) OVER ()) AS "avg"
FROM activities a
JOIN schedule s ON a.activity_code=s.activity_code
JOIN activity_bookings ab ON s.schedule_ID=ab.schedule_ID
GROUP BY activities."type";

基本上,因为每个活动预订都有一个访客 ID,我们希望获取每个活动的所有活动预订。我们必须按照时间表来做到这一点。他们我们按活动类型对行进行分组,并计算每种类型的活动预订数量。

于 2013-03-08T03:14:24.653 回答