zoukankan      html  css  js  c++  java
  • row_number() OVER (PARTITION BY COL1 ORDER BY COL2)

    select *,ROW_NUMBER() over(partition by deviceID order by RecordDate desc

    row_number() OVER (PARTITION BY COL1 ORDER BY COL2) 表示根据COL1分组,在分组内部根据 COL2排序,而此函数计算的值就表示每组内部排序后的顺序编号(组内连续的唯一的)

    实例:

    初始化数据

    create table employee (empid int ,deptid int ,salary decimal(10,2))

    insert into employee values(1,10,5500.00)

    insert into employee values(2,10,4500.00)

    insert into employee values(3,20,1900.00)

    insert into employee values(4,20,4800.00)

    insert into employee values(5,40,6500.00)

    insert into employee values(6,40,14500.00)

    insert into employee values(7,40,44500.00)

    insert into employee values(8,50,6500.00)

    insert into employee values(9,50,7500.00)

    数据显示为

    empid       deptid      salary ----------- ----------- ---------------------------------------

    1           10          5500.00

    2           10          4500.00

    3           20          1900.00

    4           20          4800.00

    5           40          6500.00

    6           40          14500.00

    7           40          44500.00

    8           50          6500.00

    9           50          7500.00

    需求:根据部门分组,显示每个部门的工资等级

    预期结果:

    empid       deptid      salary                                  rank ----------- ----------- --------------------------------------- --------------------

    1           10          5500.00                                 1

    2           10          4500.00                                 2

    4           20          4800.00                                 1

    3           20          1900.00                                 2

    7           40          44500.00                               1

    6           40          14500.00                               2

    5           40          6500.00                                 3

    9           50          7500.00                                 1

    8           50          6500.00                                 2

    SQL脚本:

    SELECT *, Row_Number() OVER (partition by deptid ORDER BY salary desc) rank FROM employee

    转自:http://www.cnblogs.com/digjim/archive/2006/09/20/509344.html

  • 相关阅读:
    二,数据类型与流程控制语句
    一,cmd指令集与变量
    web第九天,浮动与定位
    web第八天,PS切图与float浮动
    web第七天,标签分类
    web第六天,CSS优先级与盒子模型
    web第五天复合样式与选择器
    web第四天,CSS基础
    web第三天 表单与css基础
    装饰器
  • 原文地址:https://www.cnblogs.com/xiyou110/p/4444381.html
Copyright © 2011-2022 走看看