Use of the Row_number () over () analysis function in ORACLE the ___ function

Source: Internet
Author: User
Tags date1

Use of the Row_number () over () analysis function in ORACLE

Row_number () over (partition by col1 ORDER by col2) is grouped according to col1, sorted internally by col2, and the value of this function represents the sequential number after each group's internal ordering (the group is contiguous and unique).

Example:

Sql> DESC T1;
Name Null? Type
----------------------------------------- -------- ----------------------------
ID number
NAME VARCHAR2 (10)
DATE1 DATE

Sql> SELECT * from T1;
ID NAME DATE1
---------- ------------------------------ ------------------
AAA 09-SEP-13

BBB 10-sep-13

CCC 11-SEP-13

102 DDD 08-sep-13

102 Eee 11-SEP-13


Sql> SELECT Id,name,date1,row_number () over (partition by ID ORDER BY DATE1 Desc) as RN from T1;

ID NAME DATE1 RN
---------- ------------------------------ ------------------ ----------
CCC 11-SEP-13 1
BBB 10-SEP-13 2
AAA 09-SEP-13 3
102 Eee 11-SEP-13 1
102 DDD 08-SEP-13 2


Embed the above statement as a child table statement in another statement:


Sql> Select Id,name,date1 from (select Id,name,date1,row_number () over (partition by ID ORDER BY DATE1 Desc) as RN FR OM T1) T WHERE t.rn=1;

        ID name                            DATE1
----------------------------------------------------------
       CCC                                    11-sep-13
        102 eee                                    11-sep-13


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.