0

我有这些关系

Entry
-----
id
creationdate
grade


Subject
------
id
name

并加入表

Entry_Subjects
------------
entry_id
subject_id

我需要创建 sql 以查找在特定创建日期属于特定主题(例如“java”)的条目的平均成绩

我尝试了以下

假设主题“java”的 id 为 2

SELECT creationdate,
    avg(grade) 
FROM (SELECT * 
     FROM Entry 
     WHERE id IN
         (SELECT id 
          FROM Entry_Subjects 
          WHERE subject_id =2
         )
     )
GROUP BY creationdate;

我得到错误

subquery in FROM must have an alias

我试图纠正这个但不能

有人可以告诉我为什么会发生这个错误..我的数据库知识不是那么好

4

2 回答 2

1

可能需要 JOIN 而不是嵌套的 SELECT。

SELECT
    creationdate,
    AVG(grade)
FROM Entry e
INNER JOIN Entry_Subjects f
    ON e.id = f.entry_id
INNER JOIN Subject s
    ON f.subject_id = s.id
WHERE s.name = 'java' --this is where you replace 'java' with a variable to search by name
GROUP BY creationdate
于 2013-03-04T04:30:30.723 回答
0

也可以使用分析函数来完成

select a.creationdate,avg(a.grade) over (partition by a.creationdate order by a.creationdate) as avg_grade
from entry a,subject b,entry_subjects c
where a.id=c.entry_id and b.id=c.subject_id
and upper(b.name)='JAVA';
于 2013-03-04T05:20:18.847 回答