zoukankan      html  css  js  c++  java
  • SQL CTE 和 row_number 配合 用法

    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)

    select  *  from  employee

    with cte  as

    (

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

    )

    ---select  * from cte

    -----cte 的用法 相当于 一个表变量 ,把子查询的值赋予给我 cte个表  然后下面的操是针对 cte这个对象操作的,同理也是对 子查询的表操作。

    -----只留每个部门工资最高的。 原理:部门内 排序不为1的都删除。
    delete  from  cte  where RANK<>1

    ---select  *  from employee

    可随意转载,欢迎署名!
  • 相关阅读:
    python re正则表达式
    python logging模块
    python configparse模块&xml模块
    013 内置函数68个
    day011 迭代器闭包
    09 函数初识
    08 文件操作
    07 list和dict for循环遍历索引问题以及深浅拷贝
    05 字典
    04 基本数据类型(list, tuple)
  • 原文地址:https://www.cnblogs.com/netsa/p/2854437.html
Copyright © 2011-2022 走看看