关系代数
relational algebra
这是我根据我的关系构建的示例关系 table。
Assignment aID description
1 A1
2 A2
3 A3
#where aID is the unique key
Group gID aID
1 1
2 1
3 2
4 2
5 3
#where gID is the unique key , represent groups works on assignments.
Grader gID Username
1 TA1
2 TA2
3 TA1
4 TA2
5 TA1
#where gID is the unique key and represent which TA is assign to grade a group.
Result gID, mark
1 50
2 60
3 70
4 80
5 90
#where gID is the unique key and present group's mark.
找到评分每项作业的评分者,如果评分者至少给一组作业打分,我们就说评分者评分了一项作业。在我的示例中,应该报告 TA1。对这个问题有什么建议的方法吗?提前致谢。
编辑:gID 是唯一的并在所有关系中使用,意味着约束
Group[aid] /subset assignment[aid]
grader[gid] /subset group[gid]
result[gid] /subset group[gid]
不是很容易理解,我们这里没有3NF。
据我了解,只有一个 Grader 可以标记一个组,而一个组只能有一个标记。在这种情况下,您可以使用此查询:
SELECT DISTINCT Username FROM Grader WHERE Username NOT IN (
SELECT grd.Username FROM Grader grd
LEFT JOIN Assignment a ON (1=1)
LEFT JOIN (
SELECT grp.aId, grd.Username FROM [Group] grp
LEFT JOIN Grader grd ON (grp.gID = grd.gID)
LEFT JOIN Assignment a ON (grp.aID = a.aID)
LEFT JOIN Result r ON (grp.aID = r.gID)) innerQuery
ON (grd.Username = innerQuery.Username and a.aId = innerQuery.aId)
WHERE innerQuery.aId is null);
结果:
Username
TA1
请注意,当您清楚地命名事物时,事情会变得多么简单。评分者中的 gID 很容易成为评分者的 ID...它也以 g.
开头
这是一种实现我所理解的解决方案的方法。为了清楚起见,我重命名了一些东西。
declare @Assignment table (AssignmentID int, Description varchar(5))
insert into @Assignment
values
(1,'A1'),
(2,'A2'),
(3,'A3')
declare @Group table (GroupID int, AssignmentID int)
insert into @Group
values
(1,1),
(2,1),
(3,2),
(4,2),
(5,3)
declare @GroupGrader table (GroupID int, GraderName varchar(5))
insert into @GroupGrader
values
(1,'TA1'),
(2,'TA2'),
(3,'TA1'),
(4,'TA2'),
(5,'TA1')
Declare @GroupResult table (GroupID int, Mark int)
insert into @GroupResult
values
(1,50),
(2,60),
(3,70),
(4,80),
(5,90)
select gg.GraderName
from @Group g
--inner join @Assignment a
-- on a.AssignmentID = g.AssignmentID
inner join @GroupGrader gg
on g.GroupID = gg.GroupID
--inner join @GroupResult gr
-- on gr.GroupID = g.GroupID
group by gg.GraderName
having count(g.AssignmentID) = (select count(*) from @Assignment)
这是我根据我的关系构建的示例关系 table。
Assignment aID description
1 A1
2 A2
3 A3
#where aID is the unique key
Group gID aID
1 1
2 1
3 2
4 2
5 3
#where gID is the unique key , represent groups works on assignments.
Grader gID Username
1 TA1
2 TA2
3 TA1
4 TA2
5 TA1
#where gID is the unique key and represent which TA is assign to grade a group.
Result gID, mark
1 50
2 60
3 70
4 80
5 90
#where gID is the unique key and present group's mark.
找到评分每项作业的评分者,如果评分者至少给一组作业打分,我们就说评分者评分了一项作业。在我的示例中,应该报告 TA1。对这个问题有什么建议的方法吗?提前致谢。
编辑:gID 是唯一的并在所有关系中使用,意味着约束
Group[aid] /subset assignment[aid]
grader[gid] /subset group[gid]
result[gid] /subset group[gid]
不是很容易理解,我们这里没有3NF。 据我了解,只有一个 Grader 可以标记一个组,而一个组只能有一个标记。在这种情况下,您可以使用此查询:
SELECT DISTINCT Username FROM Grader WHERE Username NOT IN (
SELECT grd.Username FROM Grader grd
LEFT JOIN Assignment a ON (1=1)
LEFT JOIN (
SELECT grp.aId, grd.Username FROM [Group] grp
LEFT JOIN Grader grd ON (grp.gID = grd.gID)
LEFT JOIN Assignment a ON (grp.aID = a.aID)
LEFT JOIN Result r ON (grp.aID = r.gID)) innerQuery
ON (grd.Username = innerQuery.Username and a.aId = innerQuery.aId)
WHERE innerQuery.aId is null);
结果:
Username
TA1
请注意,当您清楚地命名事物时,事情会变得多么简单。评分者中的 gID 很容易成为评分者的 ID...它也以 g.
开头这是一种实现我所理解的解决方案的方法。为了清楚起见,我重命名了一些东西。
declare @Assignment table (AssignmentID int, Description varchar(5))
insert into @Assignment
values
(1,'A1'),
(2,'A2'),
(3,'A3')
declare @Group table (GroupID int, AssignmentID int)
insert into @Group
values
(1,1),
(2,1),
(3,2),
(4,2),
(5,3)
declare @GroupGrader table (GroupID int, GraderName varchar(5))
insert into @GroupGrader
values
(1,'TA1'),
(2,'TA2'),
(3,'TA1'),
(4,'TA2'),
(5,'TA1')
Declare @GroupResult table (GroupID int, Mark int)
insert into @GroupResult
values
(1,50),
(2,60),
(3,70),
(4,80),
(5,90)
select gg.GraderName
from @Group g
--inner join @Assignment a
-- on a.AssignmentID = g.AssignmentID
inner join @GroupGrader gg
on g.GroupID = gg.GroupID
--inner join @GroupResult gr
-- on gr.GroupID = g.GroupID
group by gg.GraderName
having count(g.AssignmentID) = (select count(*) from @Assignment)