我创建了一个用于报告学生考试历史的应用程序。
我有三个表:
CREATE TABLE [reports](
[id] [int] NOT NULL,
[examYear] [int] NULL
)
CREATE TABLE [students](
[id] [int] NOT NULL,
[examYearId] [int] NULL,
[studentname] [varchar](50) NULL
)
CREATE TABLE [classification](
[id] [int] NOT NULL,
[examYearId] [int] NULL,
[studentId] [int] NULL,
[subject] [varchar](50) NULL,
[classification] [char](1) NULL
)
我需要知道学生通过的每门科目的首次尝试和最后尝试的年份以及最终分数。
我在伪代码中的目标:
FOREACH student
select studentname, subject, firstExamYear, lastExamYear, score;
我在表中的目标
+--+----------+-------------+---------------+--------------+-----------+
|id| Student | Subject | firstExamYear | lastExamYear | score |
+--+----------+-------------+---------------+--------------+-----------+
|1 | Alice | English | 2017 | 2018 | A |
|2 | Alice | Mathematics | 2017 | 2017 | B |
|3 | Alice | Literature | 2018 | 2018 | A |
+--+----------+-------------+---------------+--------------+-----------+
爱丽丝进行了两次英语考试。 2017年的第一次尝试没有成功。但是明年爱丽丝以A分数通过。我不在乎第一次尝试(失败),重要的是她的最终分数。
请问如何在MS SQL中实现它?
您可以使用以下测试数据:
INSERT [dbo].[classification] ([id], [examYearId], [studentId], [subject], [classification]) VALUES (1, 1, 1, N'English', N'F')
GO
INSERT [dbo].[classification] ([id], [examYearId], [studentId], [subject], [classification]) VALUES (2, 1, 1, N'Mathematics', N'B')
GO
INSERT [dbo].[classification] ([id], [examYearId], [studentId], [subject], [classification]) VALUES (3, 1, 2, N'English', N'E')
GO
INSERT [dbo].[classification] ([id], [examYearId], [studentId], [subject], [classification]) VALUES (4, 1, 3, N'English', N'A')
GO
INSERT [dbo].[classification] ([id], [examYearId], [studentId], [subject], [classification]) VALUES (5, 1, 3, N'Athletics', N'B')
GO
INSERT [dbo].[classification] ([id], [examYearId], [studentId], [subject], [classification]) VALUES (6, 2, 4, N'English', N'A')
GO
INSERT [dbo].[classification] ([id], [examYearId], [studentId], [subject], [classification]) VALUES (7, 2, 4, N'Literature', N'A')
GO
INSERT [dbo].[classification] ([id], [examYearId], [studentId], [subject], [classification]) VALUES (8, 2, 4, N'Mathematics', N'C')
GO
INSERT [dbo].[classification] ([id], [examYearId], [studentId], [subject], [classification]) VALUES (9, 2, 5, N'Physics', N'B')
GO
INSERT [dbo].[classification] ([id], [examYearId], [studentId], [subject], [classification]) VALUES (10, 3, 6, N'Law', N'B')
GO
INSERT [dbo].[classification] ([id], [examYearId], [studentId], [subject], [classification]) VALUES (11, 3, 6, N'Drawing', N'A')
GO
INSERT [dbo].[classification] ([id], [examYearId], [studentId], [subject], [classification]) VALUES (12, 3, 7, N'Chemistry', N'D')
GO
INSERT [dbo].[classification] ([id], [examYearId], [studentId], [subject], [classification]) VALUES (13, 3, 8, N'Chemistry', N'A')
GO
INSERT [reports] ([id], [examYear]) VALUES (1, 2017)
GO
INSERT [reports] ([id], [examYear]) VALUES (2, 2018)
GO
INSERT [reports] ([id], [examYear]) VALUES (3, 2019)
GO
INSERT [students] ([id], [examYearId], [studentname]) VALUES (1, 1, N'Alice')
GO
INSERT [students] ([id], [examYearId], [studentname]) VALUES (2, 1, N'Bob')
GO
INSERT [students] ([id], [examYearId], [studentname]) VALUES (3, 1, N'Charlie')
GO
INSERT [students] ([id], [examYearId], [studentname]) VALUES (4, 2, N'Alice')
GO
INSERT [students] ([id], [examYearId], [studentname]) VALUES (5, 2, N'Bob')
GO
INSERT [students] ([id], [examYearId], [studentname]) VALUES (6, 2, N'Charlie')
GO
INSERT [students] ([id], [examYearId], [studentname]) VALUES (7, 3, N'Alice')
GO
INSERT [students] ([id], [examYearId], [studentname]) VALUES (8, 3, N'Eve')
GO
编辑: 我想了一段时间,我找到了使用WITH TIES的解决方案,但是它返回了废话,例如即使她在数据库中只有一条记录,它也会为Eve返回Sixt考试。
select top (1)
with ties s.studentname, c.subject, c.classification, r.examYear
from
classification c, reports r, students s
order by row_number() over (partition by
c.subject, c.studentid
order by c.classification, c.studentid desc)