zoukankan      html  css  js  c++  java
  • 开窗函数over()中partition by关键字解析

    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 desc) as 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 desc) as 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 行受影响)

  • 相关阅读:
    数据结构和算法(Golang实现)(9)基础知识-算法复杂度及渐进符号
    基于深度学习方法的dota2游戏数据分析与胜率预测(python3.6+keras框架实现)
    基于CBOW网络手动实现面向中文语料的word2vec
    《Machine Learning Yearing》读书笔记
    使用神经网络预测航班起飞准点率
    使用LSTM-RNN建立股票预测模型
    基于selenium+phantomJS的动态网站全站爬取
    TensorFlow保存、加载模型参数 | 原理描述及踩坑经验总结
    学习笔记--python中使用多进程、多线程加速文本预处理
    通过外汇对冲手段稳定获利的可行性验证
  • 原文地址:https://www.cnblogs.com/hlfei/p/3532555.html
Copyright © 2011-2022 走看看