0

这是我的两张表:

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

任何帮助,将不胜感激。一直把我的头撞在墙上试图让它工作。

4

2 回答 2

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;
于 2013-04-29T06:25:18.603 回答
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
于 2013-04-29T07:30:01.890 回答