About ROW_NUMBER(), RANK(), and DENSE_RANK() Functions in SAP HANA

About ROW_NUMBER(), RANK(), and DENSE_RANK() Functions in SAP HANA

ROW_NUMBER()

The output of the this function can be non-deterministic among tie values. The ordering of the sequence is determined by the <windows_order_by_clause> within the OVER windows clause

RANK()

This function returns duplicate values in the ranking sequence when there are ties between values and the next rankings are skipped.

DENSE_RANK()

This function is used to give rank based on measure/measures. This function is same as the RANK function, but the rank number will not skip when ties are found.

See the simple example with small data set.

Eg: We have a MARKS table like below:

Scenario 1:

Now apply functions ROW_NUMBER(), RANK() and DENSE_RANK() on above table.

Write below Code in HANA Studio in SQL Console. The below code gives first priority for Maths marks, second priority for Physics, and third priority for Chemistry.

Execute the above code and see the Results.

ROW_NUMBER() is generated numbers from 1 to 10 and allocated in sequence.

RANK() is generated Rank numbers but it skipped if students scored same marks. Students SCOTT and ADAM got same marks so for both it allocated same Rank #4. But, Rank# 5 is skipped and it allocated Rank# 6 for JAMS and JEO.

DENSE_RANK() is generated Ranks from 1 to 7. If two students scored the same marks, it allocated same Rank for both students instead of skipping Ranks.

Scenario 2:

Removing the priority on individual subjects and using the same functions on TOTAL marks, see the below code.

The result of above code is:

Scenario 3:

Apply ROW_NUMBER () function using PARTITION BY Clause.

We have PRODUCTS table with fields like Products, Color and List Price. Apply ROW_NUMBER based on COLOR (Partitioning Products based on Color) and List Price.

Note: In above code I used ORDER BY 1 ASC, it means the data will be sorted based on First Column i.e. COLOR.



Amazing explanation. Thanks a lot!

回复
Mike Taylor

Senior SAP Consultant at Forgestik Inc.

3 年

Awesome, wow this helped me tremendously. Didn't know RANK() existed. Is there an easy way to use these results in the WHERE clause? Rather than a derived table type setup? Can you call the results directly?

回复
Viral Naik

Lead Consultant - Finance Reporting (SAP Data Warehousing & Analytics)

3 年

Nice explanation!

回复
Balaji Bysani

Principal Consultant at Mercedes-Benz (BW4HANA, Native HANA XSA, SDI, S4/HANA Embedded Analytics)

4 年

Easy to Understand ??

回复

要查看或添加评论,请登录

Surendra K的更多文章

  • Data loads to SAP HANA DB using SAP DS (Data Services)

    Data loads to SAP HANA DB using SAP DS (Data Services)

    In this article I'm going explain the data loading process to SAP HANA DB using SAP ECC as Source System. SAP Data…

    13 条评论
  • AMDP in SAP HANA

    AMDP in SAP HANA

    ABAP-Managed Database Procedures (AMDP) What is AMDP? AMDP is one of the recommended patterns for use in ABAP code…

    4 条评论
  • Table Functions in SAP HANA

    Table Functions in SAP HANA

    In this article I'm going explain how to create SAP HANA Calculation View using Table Function. In my previous article…

    1 条评论
  • SAP HANA Calculation View - SQL Script

    SAP HANA Calculation View - SQL Script

    In this article I'm going explain how to create SAP HANA Calculation View using SQL Code. i.

    3 条评论
  • Calculation Engine Plan Operators (CE Functions) Vs SQL Code

    Calculation Engine Plan Operators (CE Functions) Vs SQL Code

    In this blog, I'm going explain few CE Functions and also the alternative solution for CE Functions using three tables…

  • Set Operators using SQL Code and HANA Studio GUI/WEB IDE

    Set Operators using SQL Code and HANA Studio GUI/WEB IDE

    In this article, I will explain/show all Set Operators using SQL Code and also in HANA Studio/WEB IDE using Calculation…

  • The importance of SERIES GENERATE Functions in SAP HANA

    The importance of SERIES GENERATE Functions in SAP HANA

    In this page I'm going to explain few SERIES GENERATE functions with simple examples. It looks like simple but we can…

    3 条评论

社区洞察