一、从一个熟悉的示例说起
WITH Record AS (
SELECT
Row_Number() OVER ( ORDER BY Id DESC ) AS RecordNumber,
Id,
FirstName,
LastName,
Height,
Weight
FROM
Person (NOLOCK)
)
SELECT
RecordNumber,
( SELECT COUNT ( 0 ) FROM Record) AS TotalCount,
Id,
FirstName,
LastName,
Height,
Weight
FROM Record
WHERE RecordNumber BETWEEN 1 AND 10
CREATE TABLE [ StudentScore ] (
[ Id ] [ int ] IDENTITY ( 1 , 1 ) NOT NULL ,
[ StudentId ] [ int ] NOT NULL CONSTRAINT [ DF_StudentScore_StudentId ] DEFAULT (( 0 )),
[ ClassId ] [ int ] NOT NULL CONSTRAINT [ DF_StudentScore_ClassId ] DEFAULT (( 0 )),
[ CourseId ] [ int ] NOT NULL CONSTRAINT [ DF_StudentScore_CourseId ] DEFAULT (( 0 )),
[ Score ] [ float ] NOT NULL CONSTRAINT [ DF_StudentScore_Score ] DEFAULT (( 0 )),
[ CreateDate ] [ datetime ] NOT NULL CONSTRAINT [ DF_StudentScore_CreateDate ] DEFAULT ( getdate ())
) ON [ PRIMARY ]
-- CourseId 2:语文 4:数学 8:英语
-- 1班学生成绩
INSERT INTO StudentScore(StudentId,ClassId,CourseId,Score) VALUES ( 1 , 1 , 2 , 85 )
INSERT INTO StudentScore(StudentId,ClassId,CourseId,Score) VALUES ( 2 , 1 , 2 , 95.5 )
INSERT INTO StudentScore(StudentId,ClassId,CourseId,Score) VALUES ( 3 , 1 , 2 , 90 )
INSERT INTO StudentScore(StudentId,ClassId,CourseId,Score) VALUES ( 1 , 1 , 4 , 90 )
INSERT INTO StudentScore(StudentId,ClassId,CourseId,Score) VALUES ( 2 , 1 , 4 , 98 )
INSERT INTO StudentScore(StudentId,ClassId,CourseId,Score) VALUES ( 3 , 1 , 4 , 89 )
INSERT INTO StudentScore(StudentId,ClassId,CourseId,Score) VALUES ( 1 , 1 , 8 , 80 )
INSERT INTO StudentScore(StudentId,ClassId,CourseId,Score) VALUES ( 2 , 1 , 8 , 75.5 )
INSERT INTO StudentScore(StudentId,ClassId,CourseId,Score) VALUES ( 3 , 1 , 8 , 77 )
-- 2班学生成绩
INSERT INTO StudentScore(StudentId,ClassId,CourseId,Score) VALUES ( 1 , 2 , 2 , 90 )
INSERT INTO StudentScore(StudentId,ClassId,CourseId,Score) VALUES ( 2 , 2 , 2 , 77 )
INSERT INTO StudentScore(StudentId,ClassId,CourseId,Score) VALUES ( 3 , 2 , 2 , 78 )
INSERT INTO StudentScore(StudentId,ClassId,CourseId,Score) VALUES ( 4 , 2 , 2 , 83 )
INSERT INTO StudentScore(StudentId,ClassId,CourseId,Score) VALUES ( 1 , 2 , 4 , 98 )
INSERT INTO StudentScore(StudentId,ClassId,CourseId,Score) VALUES ( 2 , 2 , 4 , 95 )
INSERT INTO StudentScore(StudentId,ClassId,CourseId,Score) VALUES ( 3 , 2 , 4 , 78 )
INSERT INTO StudentScore(StudentId,ClassId,CourseId,Score) VALUES ( 4 , 2 , 4 , 100 )
INSERT INTO StudentScore(StudentId,ClassId,CourseId,Score) VALUES ( 1 , 2 , 8 , 85 )
INSERT INTO StudentScore(StudentId,ClassId,CourseId,Score) VALUES ( 2 , 2 , 8 , 90 )
INSERT INTO StudentScore(StudentId,ClassId,CourseId,Score) VALUES ( 3 , 2 , 8 , 86 )
INSERT INTO StudentScore(StudentId,ClassId,CourseId,Score) VALUES ( 4 , 2 , 8 , 78.5 )
-- 3班学生成绩
INSERT INTO StudentScore(StudentId,ClassId,CourseId,Score) VALUES ( 1 , 3 , 2 , 82 )
INSERT INTO StudentScore(StudentId,ClassId,CourseId,Score) VALUES ( 2 , 3 , 2 , 78 )
INSERT INTO StudentScore(StudentId,ClassId,CourseId,Score) VALUES ( 3 , 3 , 2 , 91 )
INSERT INTO StudentScore(StudentId,ClassId,CourseId,Score) VALUES ( 1 , 3 , 4 , 83 )
INSERT INTO StudentScore(StudentId,ClassId,CourseId,Score) VALUES ( 2 , 3 , 4 , 78 )
INSERT INTO StudentScore(StudentId,ClassId,CourseId,Score) VALUES ( 3 , 3 , 4 , 99 )
INSERT INTO StudentScore(StudentId,ClassId,CourseId,Score) VALUES ( 1 , 3 , 8 , 86 )
INSERT INTO StudentScore(StudentId,ClassId,CourseId,Score) VALUES ( 2 , 3 , 8 , 78 )
INSERT INTO StudentScore(StudentId,ClassId,CourseId,Score) VALUES ( 3 , 3 , 8 , 97 )
SELECT
-- Id,
-- CreateDate,
StudentId,
ClassId,
CourseId,
Score,
CAST ( AVG (Score) OVER () AS decimal ( 5 , 2 ) ) AS ' 语文平均分 '
FROM
StudentScore
WHERE CourseId = 2
SELECT
Id,
CreateDate,
StudentId,
ClassId,
CourseId,
Score,
CAST ( AVG (Score) OVER (PARTITION BY ClassId ) AS decimal ( 5 , 2 ) ) AS ' 语文平均分 '
FROM
StudentScore
WHERE CourseId = 2
SELECT
Id,
-- CreateDate,
ROW_NUMBER() OVER ( ORDER BY Score DESC ) AS ' 序号 ' ,
StudentId,
ClassId,
CourseId,
Score
FROM
StudentScore
WHERE CourseId = 8
结果如下:
SELECT
Id,
-- CreateDate,
RANK() OVER ( ORDER BY Score DESC ) AS ' 序号 ' ,
StudentId,
ClassId,
CourseId,
Score
FROM
StudentScore
WHERE CourseId = 8
SELECT
Id,
-- CreateDate,
DENSE_RANK() OVER ( ORDER BY Score DESC ) AS ' 序号 ' ,
StudentId,
ClassId,
CourseId,
Score
FROM
StudentScore
WHERE CourseId = 8
SELECT
Id,
-- CreateDate,
NTILE( 6 ) OVER ( ORDER BY ClassId DESC ) AS ' 组编号 ' ,
StudentId,
ClassId,
CourseId,
Score
FROM
StudentScore
WHERE CourseId = 8