美文网首页
Row_Number()over(order by....) a

Row_Number()over(order by....) a

作者: _Irving | 来源:发表于2020-03-19 10:14 被阅读0次

1、Row_Number()over(order by....) as去重

select orderid,foodName,price ,ROW_NUMBER() OVER(PARTITION BY orderid order by price desc) from Table_3;(这样使用不会去重)

数据表先按照orderid 分组,在分组内按照price降序排序

SELECT s.* FROM (SELECT orderid,foodName,price ,ROW_NUMBER() OVER( PARTITION BY orderid ORDER BY price DESC)as rowid FROM Table_3 )s WHERE s.rowid= 1;(这样使用用去重的效果);
由查询结果可知,相同orderid 的只保留了rowid=1 的记录,其他的都被过滤掉了,从而达到了对orderid 去重处理

2、Rank() over(partition by col1 order by col2)跳跃排序
select *, rank() over(partition by orderid order by price desc) from Table_3;

3、DENSE_RANK() OVER(PARTITION BY COL1 ORDER BY COL2)连续排序
select *, dense_rank() over(partition by orderid order by price desc) from Table_3;

4、sum() over (partition by col1 order by col2)累加求和

相关文章

网友评论

      本文标题:Row_Number()over(order by....) a

      本文链接:https://www.haomeiwen.com/subject/kjskyhtx.html