Row_number() OVER(PARTITION BY xxx ORDER BY XXX)分组排序

--//创建一个信息表
CREATE TABLE user_student(id decimal(18,0) identity(1,1),st_name nvarchar(30),class nvarchar(10),score decimal(18,2))
--//插入测试数据============start===================
insert into user_student(st_name,class,score)
values(‘张三‘,‘甲‘,‘90‘)

insert into user_student(st_name,class,score)
values(‘张四‘,‘甲‘,‘65‘)

insert into user_student(st_name,class,score)
values(‘张五‘,‘甲‘,‘88‘)

insert into user_student(st_name,class,score)
values(‘李三‘,‘乙‘,‘97‘)

insert into user_student(st_name,class,score)
values(‘李四‘,‘乙‘,‘88‘)

insert into user_student(st_name,class,score)
values(‘李五‘,‘乙‘,‘78‘)

insert into user_student(st_name,class,score)
values(‘王三‘,‘丙‘,‘86‘)

insert into user_student(st_name,class,score)
values(‘王四‘,‘丙‘,‘69‘)

insert into user_student(st_name,class,score)
values(‘王五‘,‘丙‘,‘59‘)
--//插入测试数据============end===================
select * from user_student

--//每个班级分数前两名的学生信息
SELECT ST_NAME,CLASS,SCORE
FROM (
SELECT Row_number() OVER(PARTITION BY CLASS ORDER BY SCORE DESC) AS NUM,*
FROM user_student
) AS T
WHERE NUM<=2

表内数据:                   输出结果:

时间: 2024-12-30 00:05:31

Row_number() OVER(PARTITION BY xxx ORDER BY XXX)分组排序的相关文章

分组 根据某一列进行排序,根据shopid分组,用createTime排序,返回row_number()序号 select no =row_number() over (partition by shopId order by createTime desc), * from Goods_info

over不能单独使用,要和分析函数:rank(),dense_rank(),row_number()等一起使用.其参数:over(partition by columnname1 order by columnname2)含义:按columname1指定的字段进行分组排序,或者说按字段columnname1的值进行分组排序.例如:employees表中,有两个部门的记录:department_id =10和20select department_id,rank() over(partition

row_number() OVER (PARTITION BY COL1 ORDER BY COL2)

--表示根据COL1分组,在分组内部根据 COL2排序,而此函数返回的值就表示每组内部排序后的顺序编号(组内连续的唯一的) create table student (id int ,classes int ,score int); insert into student values(1,1,89); insert into student values(2,1,90); insert into student values(3,1,76); insert into student value

row_number() over (partition by....order by...)用法 分组排序

转载来源:http://www.cnblogs.com/Kazaf/archive/2011/06/30/2094015.html row_number() OVER (PARTITION BY COL1 ORDER BY COL2) 表示根据COL1分组,在分组内部根据 COL2排序,而此函数计算的值就表示每组内部排序后的顺序编号(组内连续的唯一的) SELECT G.*, ROW_NUMBER() OVER(PARTITION BY a ,b ORDER BY c DESC) ROWN fr

ROW_NUMBER() OVER(PARTITION BY COLUMN ORDER BY COLUMN DESC)函数的使用

ROW_NUMBER() OVER(PARTITION BY COLUMN ORDER BY COLUMN DESC)函数的作用是指定COLUMN(列)进行分区,在分区内指定COLUMN(列)进行排序,其中PARTITION BY COLUMN 为分区函数.代码具体实现如下 不进行分区的排序方式--对商品价格的升序排序 SELECT A.*, ROW_NUMBER() OVERORDER BY PRICE ASC) AS ROW_FLG FROM COMMODITY A 进分区的排序方式--对商

oracle常用分析函数 over(partition by xxx order by xxx)

--over order by 连续累加的意思,把by后面相同的字段,一个组组累加起来SELECT id_,name_,proc_def_id_, count(*) over(order by name_) FROM ACT_RU_TASK --over partition by 分组统计 类似于group by,区别是它会列出重复的项,而group by只列出一条SELECT id_,name_,proc_def_id_, count(*) over(partition by name_) F

row_number() over(partition by a order by b desc) rn 用法

ow_number() OVER (PARTITION BY COL1 ORDER BY COL2) 表示根据COL1分组,在分组内部根据 COL2排序,而此函数计算的值就表示每组内部排序后的顺序编号(组内连续的唯一的) 实际应用 取最新的数据 select * from (select hlpr.emplid, hlpr.deptid, hlpr.create_time, row_number() over (partition by hlpr.emplid order by hlpr.cre

去重 ROW_NUMBER() OVER(PARTITION BY 分组字段 ORDER BY 排序字段) RN

关键字  ROW_NUMBER() OVER(PARTITION BY 分组字段 ORDER BY 排序字段) RN 按照分组字段进行排序并标编号 ROW_NUMBER() OVER(PARTITION BY 分组字段 ORDER BY 排序字段) RN 语法 ROW_NUMBER() OVER(PARTITION BY XMJL_UID ORDER BY XMCREATETIME) RN 实例 SELECT XMJL_UID,XMJL,XMCREATETIME,ROW_NUMBER() OVE

Oracle中的 row_number() over (partition by order by ) 用法

oracle 里面经常这样用 select col1,col2..., row_number() over (partition by colx order by coly) from table_name;; 这句话的意思是把表中的数值按照colx 分组,每一组内部按照coly排序,同时 row_number()返回排序之后该记录在改组内部的序号. 比如我们知道有emp表如下: SQL> SELECT * FROM SCOTT.EMP; EMPNO ENAME JOB MGR HIREDATE

【SQL】ROW_NUMBER() OVER(partition by 分组列 order by 排序列)用法详解+经典实例

原文:[SQL]ROW_NUMBER() OVER(partition by 分组列 order by 排序列)用法详解+经典实例 #用法说明 select row_number() over(partition by A order by B ) as rowIndex from table A :为分组字段 B:为分组后的排序字段. table 表的结构 多为:  多人 多条的相关数据.(比如:订单信息) 此条sql语句,多用于对数据进行分组排序,并对每个组中的数据分别进行编号,编号从1开始