我有这些关系
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
我试图纠正这个但不能
有人可以告诉我为什么会出现这种错误..我的数据库知识不是那么好
答案 0 :(得分: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
答案 1 :(得分: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';