以下是我的两张表:
CREATE TABLE Classes (
Class_ID Varchar2(2) CONSTRAINT Classes_Class_ID_Pk PRIMARY KEY,
Class_Name VARCHAR(15) CONSTRAINT Classes_Class_Name_NN NOT NULL,
Price Number(5, 2),
Start_Date DATE,
End_Date DATE,
Start_Time Varchar2(5),
End_Time Varchar2(5),
Day VARCHAR(10),
Trainer_ID CONSTRAINT Classes_Trainer_ID_FK REFERENCES Trainers(Trainer_ID)
);
CREATE TABLE ClassSchedule (
Class_ID Varchar2(2) CONSTRAINT Classes_Class_ID_FK REFERENCES Classes(Class_ID),
Member_ID Varchar2(2) CONSTRAINT Members_Member_ID_FK REFERENCES Members(Member_ID),
Paid CHAR(1),
CONSTRAINT ClassSchedule_Combo_PK PRIMARY KEY (Class_ID, Member_ID)
);
现在,对于每个课程,我想列出课程ID,课程名称,每个班级的价格,每个班级的成员数量以及每个班级的总收入。我只想显示超过400美元的那些组,并按计算的总数对它们进行排序。这是我的查询不起作用:
SELECT
C.Class_Id,
Class_Name,
Price,
Count(S.Member_Id) AS Student_Count,
(Student_Count * Price) AS Class_Revenue
FROM Classes C,
ClassSchedule S
WHERE C.Class_ID = S.Class_Id
AND (Student_Count * Price) > 400
ORDER BY Class_Revenue DESC
任何帮助将不胜感激。一直在撞墙试图让它发挥作用。
答案 0 :(得分:0)
select Class_Id,Class_Name,Price,Student_Count*Price Class_Revenue from
(Select
C.Class_Id,
Class_Name,
Price,
Count(S.Member_Id) As Student_Count
From Classes C, ClassSchedule S
Where C.Class_ID = S.Class_Id
group by C.Class_Id,
Class_Name,
Price
)
where having (Student_Count*Price) > 400
Order By Class_Revenue Desc;
答案 1 :(得分:0)
尝试在over
上使用count()
子句:
select Class_Id,
Class_Name,
Price,
Student_Count,
(Student_Count * Price) AS Class_Revenue
from
(SELECT
C.Class_Id,
Class_Name,
Price,
Count(S.Member_Id) over (partition by C.Class_Id) AS Student_Count
FROM Classes C,
ClassSchedule S
WHERE C.Class_ID = S.Class_Id) t
where (Student_Count * Price) > 400
order by (Student_Count * Price) desc