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 values(4,2,69);
insert into student values(5,2,79);
insert into student values(6,2,95);
insert into student values(7,3,80);
insert into student values(8,3,85);
insert into student values(9,3,79);
commit;
select t.* from student t;

--数据显示为

id       classes      score

-------------------------------------------------------------

1           1          89

2           1          90

3           1          76

4           2          69

5           2          79

6           2          95

7           3          80

8           3          85

9           3          79

--需求:根据班级分组,显示每个班的英语成绩排名

--预期结果:

id       classes      score                              rank

----------- ----------- ---------------------------------------

3           1          76                                 1

1           1          89                                 2

2           1          90                                 3

4           2          69                                 1

5           2          79                                 2

6           2          95                                 3

9           3          79                                 1

7           3          80                                 2

8           3          85                                 3

--SQL脚本:

SELECT *, Row_Number() OVER (partition by classes ORDER BY score desc) rank FROM student;

--查询t_test表中,callid字段没有重复过的数据,效率高过group by having count

select t.*, t.rowid
  from t_test t
 where t.rowid not in (select rid
                         from (select t2.rowid rid,
                                      row_number() over(partition by t2.callid order by t2.rowid desc) m
                                 from t_test t2)
                        where m <> 1)
   and t.rowid not in (select rid
                         from (select t2.rowid rid,
                                      row_number() over(partition by t2.callid order by t2.rowid asc) m
                                 from t_test t2)
                        where m <> 1);

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

时间: 2024-10-25 04:46:34

row_number() OVER (PARTITION BY COL1 ORDER BY COL2)的相关文章

ROWNUMBER() OVER( PARTITION BY COL1 ORDER BY COL2)用法,先分组,然后在组内排名,分组计算等

ROWNUMBER() OVER( PARTITION BY COL1 ORDER BY COL2)用法 今天在使用多字段去重时,由于某些字段有多种可能性,只需根据部分字段进行去重,在网上看到了rownumber() over(partition by col1 order by col2)去重的方法,很不错,在此记录分享下: row_number() OVER ( PARTITION BY COL1 ORDER BY COL2) 表示根据COL1分组,在分组内部根据 COL2排序,而此函数计算

转转转---ROWNUMBER() OVER( PARTITION BY COL1 ORDER BY COL2)用法

ROWNUMBER() OVER( PARTITION BY COL1 ORDER BY COL2)用法 http://blog.csdn.net/yinshan33/article/details/18738229 今天在使用多字段去重时,由于某些字段有多种可能性,只需根据部分字段进行去重,在网上看到了rownumber() over(partition by col1 order by col2)去重的方法,很不错,在此记录分享下: row_number() OVER ( PARTITION

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 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') inser

分组 根据某一列进行排序,根据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 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 进分区的排序方式--对商

Row_number over (Partition by xxx order by yyy) in Linq?

public static void Main(string[] args) { var beatles = (new[] { new { id=1 , inst = "guitar" , name="john" }, new { id=2 , inst = "guitar" , name="george" }, new { id=3 , inst = "guitar" , name="paul&

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

sql 分组取最新的数据sqlserver巧用row_number和partition by分组取top数据

SQL Server 2005后之后,引入了row_number()函数,row_number()函数的分组排序功能使这种操作变得非常简单 分组取TOP数据是T-SQL中的常用查询, 如学生信息管理系统中取出每个学科前3名的学生.这种查询在SQL Server 2005之前,写起来很繁琐,需要用到临时表关联查询才能取到.SQL Server 2005后之后,引入了row_number()函数,row_number()函数的分组排序功能使这种操作变得非常简单.下面是一个简单示例: --1.创建测试