SQL 使用 PIVOT 查询

SQL query with PIVOT

数据:

CREATE TABLE [dbo].[Grade_Report](
    [Student] [varchar](50) NULL,
    [Subject] [varchar](50) NULL,
    [Grades] [numeric](18, 2) NULL,
    [Semester] [int] NULL
);
INSERT [dbo].[Grade_Report] ([Student], [Subject], [Grades], [Semester]) VALUES (N'Jamie', N'Mathematics', CAST(82.00 AS Numeric(18, 2)), 1);
INSERT [dbo].[Grade_Report] ([Student], [Subject], [Grades], [Semester]) VALUES (N'Jamie', N'Science', CAST(83.00 AS Numeric(18, 2)), 1);
INSERT [dbo].[Grade_Report] ([Student], [Subject], [Grades], [Semester]) VALUES (N'Jamie', N'Programming', CAST(91.00 AS Numeric(18, 2)), 1);
INSERT [dbo].[Grade_Report] ([Student], [Subject], [Grades], [Semester]) VALUES (N'Jamie', N'History', CAST(89.00 AS Numeric(18, 2)), 1);
INSERT [dbo].[Grade_Report] ([Student], [Subject], [Grades], [Semester]) VALUES (N'Jamie', N'English', CAST(95.00 AS Numeric(18, 2)), 1);
INSERT [dbo].[Grade_Report] ([Student], [Subject], [Grades], [Semester]) VALUES (N'Jamie', N'Mathematics', CAST(79.00 AS Numeric(18, 2)), 2);
INSERT [dbo].[Grade_Report] ([Student], [Subject], [Grades], [Semester]) VALUES (N'Jamie', N'Science', CAST(80.00 AS Numeric(18, 2)), 2);
INSERT [dbo].[Grade_Report] ([Student], [Subject], [Grades], [Semester]) VALUES (N'Jamie', N'Programming', CAST(92.00 AS Numeric(18, 2)), 2);
INSERT [dbo].[Grade_Report] ([Student], [Subject], [Grades], [Semester]) VALUES (N'Jamie', N'History', CAST(95.00 AS Numeric(18, 2)), 2);
INSERT [dbo].[Grade_Report] ([Student], [Subject], [Grades], [Semester]) VALUES (N'Jamie', N'English', CAST(93.00 AS Numeric(18, 2)), 2);
INSERT [dbo].[Grade_Report] ([Student], [Subject], [Grades], [Semester]) VALUES (N'Jenny', N'Mathematics', CAST(95.00 AS Numeric(18, 2)), 1);
INSERT [dbo].[Grade_Report] ([Student], [Subject], [Grades], [Semester]) VALUES (N'Jenny', N'Science', CAST(95.00 AS Numeric(18, 2)), 1);
INSERT [dbo].[Grade_Report] ([Student], [Subject], [Grades], [Semester]) VALUES (N'Jenny', N'Programming', CAST(90.00 AS Numeric(18, 2)), 1);
INSERT [dbo].[Grade_Report] ([Student], [Subject], [Grades], [Semester]) VALUES (N'Jenny', N'History', CAST(92.00 AS Numeric(18, 2)), 1);
INSERT [dbo].[Grade_Report] ([Student], [Subject], [Grades], [Semester]) VALUES (N'Jenny', N'English', CAST(96.00 AS Numeric(18, 2)), 1);
INSERT [dbo].[Grade_Report] ([Student], [Subject], [Grades], [Semester]) VALUES (N'Jenny', N'Mathematics', CAST(78.00 AS Numeric(18, 2)), 2);
INSERT [dbo].[Grade_Report] ([Student], [Subject], [Grades], [Semester]) VALUES (N'Jenny', N'Science', CAST(79.00 AS Numeric(18, 2)), 2);
INSERT [dbo].[Grade_Report] ([Student], [Subject], [Grades], [Semester]) VALUES (N'Jenny', N'Programming', CAST(82.00 AS Numeric(18, 2)), 2);
INSERT [dbo].[Grade_Report] ([Student], [Subject], [Grades], [Semester]) VALUES (N'Jenny', N'History', CAST(80.00 AS Numeric(18, 2)), 2);
INSERT [dbo].[Grade_Report] ([Student], [Subject], [Grades], [Semester]) VALUES (N'Jenny', N'English', CAST(79.00 AS Numeric(18, 2)), 2);
INSERT [dbo].[Grade_Report] ([Student], [Subject], [Grades], [Semester]) VALUES (N'Jerome', N'Mathematics', CAST(96.00 AS Numeric(18, 2)), 1);
INSERT [dbo].[Grade_Report] ([Student], [Subject], [Grades], [Semester]) VALUES (N'Jerome', N'Science', CAST(92.00 AS Numeric(18, 2)), 1);
INSERT [dbo].[Grade_Report] ([Student], [Subject], [Grades], [Semester]) VALUES (N'Jerome', N'Programming', CAST(93.00 AS Numeric(18, 2)), 1);
INSERT [dbo].[Grade_Report] ([Student], [Subject], [Grades], [Semester]) VALUES (N'Jerome', N'History', CAST(88.00 AS Numeric(18, 2)), 1);
INSERT [dbo].[Grade_Report] ([Student], [Subject], [Grades], [Semester]) VALUES (N'Jerome', N'English', CAST(94.00 AS Numeric(18, 2)), 1);
INSERT [dbo].[Grade_Report] ([Student], [Subject], [Grades], [Semester]) VALUES (N'Jerome', N'Mathematics', CAST(90.00 AS Numeric(18, 2)), 2);
INSERT [dbo].[Grade_Report] ([Student], [Subject], [Grades], [Semester]) VALUES (N'Jerome', N'Science', CAST(92.00 AS Numeric(18, 2)), 2);
INSERT [dbo].[Grade_Report] ([Student], [Subject], [Grades], [Semester]) VALUES (N'Jerome', N'Programming', CAST(98.00 AS Numeric(18, 2)), 2);
INSERT [dbo].[Grade_Report] ([Student], [Subject], [Grades], [Semester]) VALUES (N'Jerome', N'History', CAST(93.00 AS Numeric(18, 2)), 2);
INSERT [dbo].[Grade_Report] ([Student], [Subject], [Grades], [Semester]) VALUES (N'Jerome', N'English', CAST(90.00 AS Numeric(18, 2)), 2);


SELECT * FROM Grade_Report;

查询:

SELECT Student, [English], [Mathematics], [Science], [Programming], [History]
FROM
(
    SELECT Grades, Subject FROM Grade_Report
)AS SourceTable
PIVOT
(
    AVG(Grades)
    FOR Subject IN([English], [Mathematics], [Science], [Programming], [History])
)AS PivotTable;

Student 突出显示为无效列名,当我从查询中删除 Student 并执行时,输出为:

我尝试在查询中以任何方式放入和取出学生列,但它给出了错误。我想要的输出是将科目显示在列中,并将显示两个学期学生的计算平均成绩。像这样:

将学生姓名放入子查询
从 ( SELECT 学生, 成绩,科目来自 Grade_Report )AS 源表

SELECT Student, [English], [Mathematics], [Science], [Programming], 
[History]
FROM
(
    SELECT Student, Grades, Subject FROM Grade_Report
)AS SourceTable
PIVOT
(
    AVG(Grades)
    FOR Subject IN([English], [Mathematics], [Science], [Programming], 
    [History])
)AS PivotTable;