SQL 语句-partition by

/******  ******/
初始化数据

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

SQL脚本:

SELECT *
FROM
    (SELECT *, Row_Number() OVER (partition by deptid ORDER BY salary desc) R
     FROM employee) B
WHERE B.R =1

#只显示部门工资最高的
/******  ******/
 select *
  from (
  select name,学科,成绩,ROW_NUMBER() over (PARTITION by 学科 order by 成绩 desc) as num
  from [XSBOOK].[dbo].[XKCJ]) T
  where T.num <= 3 order by T.学科
  
name       学科         成绩          num
---------- ---------- ----------- --------------------
李四         数学         98          1
李四         数学         95          2
张三         数学         93          3
张三         英语         92          1
张三         英语         92          2
李四         英语         90          3
李四         语文         85          1
李四         语文         81          2
张三         语文         80          3

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

转载于:https://www.cnblogs.com/li-volleyball/p/5481864.html

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值