zoukankan      html  css  js  c++  java
  • ROW_NUMBER() OVER函数的基本用法,也可用于去除重复行

    语法:ROW_NUMBER() OVER(PARTITION BY COLUMN ORDER BY COLUMN)

    简单的说row_number()从1开始,为每一条分组记录返回一个数字,这里的ROW_NUMBER() OVER (ORDER BY xlh DESC) 是先把xlh列降序,再为降序以后的没条xlh记录返回一个序号。 
    示例: 
    xlh           row_num 
    1700              1 
    1500              2 
    1085              3 
    710                4

    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

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

    SQL脚本:

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

    预期结果:

    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

    去除部门重复行

    select empid,deptid,rank from
    (SELECT *, Row_Number() OVER (partition by deptid ORDER BY salary desc) rank FROM employee) a
    where rank<=1

    预期结果:

    empid       deptid                                rank
    ----------- ----------- ------------------------------
    1           10                                          1
    4           20                                          1
    7           40                                          1
    9           50                                          1

    参:ROW_NUMBER() OVER函数的基本用法用法

  • 相关阅读:
    iOS之const,static,extern简介
    ubuntu14.04安装pycurl报错: __main__.ConfigurationError: Could not run curl-config: [Errno 2] No such file or directory
    Django过滤器列表
    ubuntu 开机显示错误:无法应用原保存的显示器配置
    安装 ppsycopg2报错, Error: You need to install postgresql-server-dev-X.Y for building a server-side extension or libpq-dev for building a client-side application
    [已解决]EnvironmentError: mysql_config not found
    Ubuntu14.04安装MySQL-python异常: mysql_config: not found,Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-MJWMPd/MySQL-python/
    Ubuntu14.04通过pyenv配置多python
    【原创】No matching distribution found for Twisted>=10.0.0 (from scrapy)
    [原创] 更新Ubuntu自带的python2.X版本 ImportError: No module named pip;ImportError: No module named _sqlite3
  • 原文地址:https://www.cnblogs.com/wangfuyou/p/6803588.html
Copyright © 2011-2022 走看看