zoukankan      html  css  js  c++  java
  • SQL技术内幕-4 row_number() over( partition by XX order by XX)的用法(区别于group by 和order by)

    【转载】原文章来自于 http://www.cnblogs.com/alphafly/p/4233759.html

    partition  by关键字是分析性函数的一部分,它和聚合函数不同的地方在于它能返回一个分组中的多条记录,而聚合函数一般只有一条反映统计值的记录,partition  by用于给结果集分组,如果没有指定那么它把整个结果集作为一个分组

     
    create database StudentDB
    go
     
    use StudentDB
    go
     
    create table Student  --学生成绩表
    (
     id int,  --主键
     Grade int--班级
     Score int --分数
    )
    go
     
    insert Student
        select 1,1,88
    union all select 2,1,66
    union all select 3,1,75
    union all select 4,2,30
    union all select 5,2,70
    union all select 6,2,80
    union all select 7,2,60
    union all select 8,3,90
    union all select 9,3,70
    union all select 10,3,80
     
    go
     
    --所有学生信息
    select from Student
     
    id          Grade       Score
    ----------- ----------- -----------
    1           1           88
    2           1           66
    3           1           75
    4           2           30
    5           2           70
    6           2           80
    7           2           60
    8           3           90
    9           3           70
    10          3           80
     
    (10 行受影响)
     
    --不分班按学生成绩排名
    select *,ROW_NUMBER() over(order by Score descas Sequence from Student
     
    id          Grade       Score       Sequence
    ----------- ----------- ----------- --------------------
    8           3           90          1
    1           1           88          2
    6           2           80          3
    10          3           80          4
    3           1           75          5
    9           3           70          6
    5           2           70          7
    2           1           66          8
    7           2           60          9
    4           2           30          10
     
    (10 行受影响)
     
    --分班后按学生成绩排名
    select *,row_number() over(partition by Grade order by Score descas Sequence from Student
     
    id          Grade       Score       Sequence
    ----------- ----------- ----------- --------------------
    1           1           88          1
    3           1           75          2
    2           1           66          3
    6           2           80          1
    5           2           70          2
    7           2           60          3
    4           2           30          4
    8           3           90          1
    10          3           80          2
    9           3           70          3
     
    (10 行受影响)
     
    个人总结:order by排序时对partition by 了的字段无效。
     
  • 相关阅读:
    SQL2005 SQL2008 远程连接配置方法
    Subvision 安装 部署 TortoiseSVN
    在wpf或winform关闭子窗口或对子窗口进行某个操作后刷新父窗口
    C# 中的委托和事件
    长数字隔三位用逗号","隔开,保留两位小数,指定长度,不足补空格
    C# 柱状图, 折线图, 扇形图
    如何在Visual Studio 2010旗舰版本下安装Window Phone 7 简体中文开发环境
    vs2010发布、打包安装程序(超全超详细)
    java 环境搭建
    SQL2008 转 2000(高版本转换到低版本)
  • 原文地址:https://www.cnblogs.com/ghw0501/p/5900644.html
Copyright © 2011-2022 走看看