sql server 2005中的分区函数用法(partition by 字段)

来源:互联网 发布:informix批量生成数据 编辑:程序博客网 时间:2024/05/19 13:29
        partition  by关键字是分析性函数的一部分,它和聚合函数不同的地方在于它能返回一个分组中的多条记录,而聚合函数一般只有一条反映统计值的记录,partition  by用于给结果集分组,如果没有指定那么它把整个结果集作为一个分组
create database StudentDBgouse StudentDBgocreate table Student  --学生成绩表( id int,  --主键 Grade int, --班级 Score int --分数)goinsert Student    select 1,1,88union all select 2,1,66union all select 3,1,75union all select 4,2,30union all select 5,2,70union all select 6,2,80union all select 7,2,60union all select 8,3,90union all select 9,3,70union all select 10,3,80go--所有学生信息select * from Studentid          Grade       Score----------- ----------- -----------1           1           882           1           663           1           754           2           305           2           706           2           807           2           608           3           909           3           7010          3           80(10 行受影响)--不分班按学生成绩排名select *,ROW_NUMBER() over(order by Score desc) as Sequence from Studentid          Grade       Score       Sequence----------- ----------- ----------- --------------------8           3           90          11           1           88          26           2           80          310          3           80          43           1           75          59           3           70          65           2           70          72           1           66          87           2           60          94           2           30          10(10 行受影响)--分班后按学生成绩排名select *,row_number() over(partition by Grade order by Score desc) as Sequence from Studentid          Grade       Score       Sequence----------- ----------- ----------- --------------------1           1           88          13           1           75          22           1           66          36           2           80          15           2           70          27           2           60          34           2           30          48           3           90          110          3           80          29           3           70          3


原创粉丝点击