建站学 - 轻松建站从此开始!

建站学-个人建站指南,网页制作,网站设计,网站制作教程

当前位置: 建站学 > 数据库 > Oracle教程 >

Oracle之ROW_NUMBER() OVER函数

时间:2018-11-18 13:12来源:未知 作者:admin 点击:
语法: ROW_NUMBER() OVER ( ORDER BY COLUMN) 简单的说row_number()从1开始,为每一条分组记录返回一个数字,这里的select ACD_ID,ROW_NUMBER() OVER(ORDER BY ACD_ID desc) from cc_acdinfo是先把ACD_ID列降序,再为降序以后的每条ACD_ID记录返回一个序号

 语法:ROW_NUMBER() OVER(ORDER BY COLUMN)

  简单的说row_number()从1开始,为每一条分组记录返回一个数字,这里的select ACD_ID,ROW_NUMBER() OVER(ORDER BY ACD_ID desc) from cc_acdinfo是先把ACD_ID列降序,再为降序以后的每条ACD_ID记录返回一个序号。 

  "ACD_ID"         "ROW_NUMBER()OVER(ORDERBYACD_ID"
       "174"                       "1"
       "154"                       "2"
       "114"                       "3"
        "75"                        "4"

  • row_number() OVER (PARTITION BY COL1 ORDER BY COL2) 表示根据COL1分组,在分组内部根据 COL2排序,而此函数计算的值就表示每组内部排序后的顺序编号(组内连续的唯一的)

  • 实例:

    初始化数据

    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)

     

    select deptid,salary, row_number() OVER (PARTITION BY deptid ORDER BY salary) from employee

    数据显示为:

    "DEPTID"    ,"SALARY",        "ROW_NUMBER()OVER(PARTITIONBYDE"
    "10",          "4500.00",                                  "1"
    "10",          "5500.00",                                  "2"
    "20",         "1900.00",                                   "1"
    "20",         "4800.00",                                   "2"
    "40",          "6500.00",                                  "1"
    "40",          "14500.00",                                "2"
    "40",         "44500.00",                                 "3"
    "50",          "6500.00",                                  "1"
    "50",          "7500.00",                                  "2"

     


(责任编辑:admin)
织梦二维码生成器
顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:点击我更换图片