Home >Database >Mysql Tutorial >Oracle关键字Over、With用法

Oracle关键字Over、With用法

WBOY
WBOYOriginal
2016-06-07 17:22:011077browse

row_number()over(partition by col1 order by col2)表示根据col1分组,在分组内部根据col2排序,而此函数计算的值就表示每组内部

一:

row_number()over(partition by col1 order by col2)表示根据col1分组,在分组内部根据col2排序,,而此函数计算的值就表示每组内部排序后的顺序编号(组内连续的唯一的)

row_number()和rownum差不多,功能更强一点(可以在各个分组内从1开始排序)。

rank()是跳跃排序,有两个第二名时接下来就是第四名(同样是在各个分组内)

dense_rank()也是连续排序,有两个第二名时仍然跟着第三名。相比之下row_number是没有重复值的。

1例:

select row_number() over(partition by orgid order by userid) as row_ ,username,orgid,userid from au_userinfo;

相当于根据orgid分组,然后给每组进行排名;

结果如下:

2例:

select row_number() over(order by userid) as a, orgid,userid,username from au_userinfo;

相当于 order by userid排序;

二:

with用法:相当于一个结果集表名:

with aaaa as (select userid,orgid,username from au_userinfo) select userid,username from aaaa;

结果如下:

linux

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn