OceanBase logo

OceanBase

A unified distributed database ready for your transactional, analytical, and AI workloads.

DEPLOY YOUR WAY

OceanBase Cloud

The best way to deploy and scale OceanBase

OceanBase Enterprise

Run and manage OceanBase on your infra

TRY OPEN SOURCE

OceanBase Community Edition

The free, open-source distributed database

OceanBase seekdb

Open source AI native search database

Customer Stories

Real-world success stories from enterprises across diverse industries.

View All
BY USE CASES

Mission-Critical Transactions

Global & Multicloud Application

Elastic Scaling for Peak Traffic

Real-time Analytics

Active Geo-redundancy

Database Consolidation

Resources

Comprehensive knowledge hub for OceanBase.

Blog

Live Demos

Training & Certification

Documentation

Official technical guides, tutorials, API references, and manuals for all OceanBase products.

View All
PRODUCTS

OceanBase Cloud

OceanBase Database

Tools

Connectors and Middleware

QUICK START

OceanBase Cloud

OceanBase Database

BEST PRACTICES

Practical guides for utilizing OceanBase more effectively and conveniently

Company

Learn more about OceanBase – our company, partnerships, and trust and security initiatives.

About OceanBase

Partner

Trust Center

Contact Us

International - English
中国站 - 简体中文
日本 - 日本語
Sign In
Start on Cloud

A unified distributed database ready for your transactional, analytical, and AI workloads.

DEPLOY YOUR WAY

OceanBase Cloud

The best way to deploy and scale OceanBase

OceanBase Enterprise

Run and manage OceanBase on your infra

TRY OPEN SOURCE

OceanBase Community Edition

The free, open-source distributed database

OceanBase seekdb

Open source AI native search database

Customer Stories

Real-world success stories from enterprises across diverse industries.

View All
BY USE CASES

Mission-Critical Transactions

Global & Multicloud Application

Elastic Scaling for Peak Traffic

Real-time Analytics

Active Geo-redundancy

Database Consolidation

Comprehensive knowledge hub for OceanBase.

Blog

Live Demos

Training & Certification

Documentation

Official technical guides, tutorials, API references, and manuals for all OceanBase products.

View All
PRODUCTS
OceanBase CloudOceanBase Database
ToolsConnectors and Middleware
QUICK START
OceanBase CloudOceanBase Database
BEST PRACTICES

Practical guides for utilizing OceanBase more effectively and conveniently

Learn more about OceanBase – our company, partnerships, and trust and security initiatives.

About OceanBase

Partner

Trust Center

Contact Us

Start on Cloud
编组
All Products
    • Databases
    • iconOceanBase Database
    • iconOceanBase Cloud
    • iconOceanBase Tugraph
    • iconInteractive Tutorials
    • iconOceanBase Best Practices
    • Tools
    • iconOceanBase Cloud Platform
    • iconOceanBase Migration Service
    • iconOceanBase Developer Center
    • iconOceanBase Migration Assessment
    • iconOceanBase Admin Tool
    • iconOceanBase Loader and Dumper
    • iconOceanBase Deployer
    • iconKubernetes operator for OceanBase
    • iconOceanBase Diagnostic Tool
    • iconOceanBase Binlog Service
    • Connectors and Middleware
    • iconOceanBase Database Proxy
    • iconEmbedded SQL in C for OceanBase
    • iconOceanBase Call Interface
    • iconOceanBase Connector/C
    • iconOceanBase Connector/J
    • iconOceanBase Connector/ODBC
    • iconOceanBase Connector/NET
icon

OceanBase Database

SQL - V4.2.1

    Download PDF

    OceanBase logo

    The Unified Distributed Database for the AI Era.

    Follow Us
    Products
    OceanBase CloudOceanBase EnterpriseOceanBase Community EditionOceanBase seekdb
    Resources
    DocsBlogLive DemosTraining & Certification
    Company
    About OceanBaseTrust CenterLegalPartnerContact Us
    Follow Us

    © OceanBase 2026. All rights reserved

    Cloud Service AgreementPrivacy PolicySecurity
    Contact Us
    Document Feedback
    1. Documentation Center
    2. OceanBase Database
    3. SQL
    4. V4.2.1
    iconOceanBase Database
    SQL - V 4.2.1
    SQL
    KV
    • V 4.4.2
    • V 4.3.5
    • V 4.3.3
    • V 4.3.1
    • V 4.3.0
    • V 4.2.5
    • V 4.2.2
    • V 4.2.1
    • V 4.2.0
    • V 4.1.0
    • V 4.0.0
    • V 3.1.4 and earlier

    CUME_DIST

    Last Updated:2023-12-25 08:49:42  Updated
    share
    What is on this page
    Purpose
    Syntax
    Parameters
    Return type
    Examples
    Example of an aggregate function
    Example of an analytic function

    folded

    share

    Purpose

    CUME_DIST() calculates the cumulative distribution of a value in a group of values. The result is in the range of (0, 1]. You can use it as an aggregate or analytic function.

    Note

    • As an analytic function, it calculates the relative position of a specified value in a group of values. For row, assume that the ascending order is used. cume_dist of row divides the number of rows with a value less than or equal to row by the number of rows computed (in the entire query result set or partition).
    • As an aggregate function, the arguments of the function identify a single hypothetical row in each aggregate group. Therefore, they must all be evaluated as constant expressions within each aggregate group. The constant argument expressions match the expressions in the ORDER BY aggregate clause by position. Therefore, the number of arguments must be the same and their types must be compatible. If the specified value is duplicate with a value in the ordered group, the two duplicate values are treated as one value.

    Syntax

    /*Aggregate syntax*/
    CUME_DIST( expr[,expr]...)
      WITHIN GROUP (ORDER BY expr_col [ DESC | ASC ][ NULLS { FIRST | LAST } ]
                           [,expr_col [ DESC | ASC ][ NULLS { FIRST | LAST } ]]...
              )
    
    /*Analytic syntax*/
    CUME_DIST() OVER ([ query_partition_clause ] order_by_clause)
    

    Parameters

    Parameter Description
    expr The value to query.
    expr_col The column name of the value to query.
    DESC | ASC Optional. The sorting method of the list.
    • ASC specifies to sort in ascending order. It is the default value.
    • DESC specifies to sort in descending order.
    NULLS { FIRST | LAST } Optional. The position of NULL in expr_col after sorting.
    • NULLS FIRST indicates that NULLs are sorted before non-NULL values.
    • NULLS LAST indicates that NULLs are sorted after non-NULL values.
    OVER You can use the OVER clause to define a window over the data on which the function operates. For more information, see Analytic functions.

    Return type

    The return type is NUMBER.

    Examples

    The table emp_msg has been created.

    obclient> SELECT * FROM emp_msg;
    +--------+--------+------+------+
    | DEPTNO | ENAME  | SAL  | MGR  |
    +--------+--------+------+------+
    |     10 | CLARK  | 2750 | 7839 |
    |     10 | KING   | 5300 | NULL |
    |     10 | MILLER | 1600 | 7782 |
    |     20 | ADAMS  | 1400 | 7788 |
    |     20 | FORD   | 3300 | 7566 |
    |     20 | JONES  | 3275 | 7839 |
    |     20 | SCOTT  | 3300 | 7566 |
    |     20 | SMITH  | 1100 | 7902 |
    |     30 | ALLEN  | 1900 | 7698 |
    |     30 | BLAKE  | 3150 | 7839 |
    |     30 | JAMES  | 1250 | 7698 |
    |     30 | MARTIN | 1550 | 7698 |
    |     30 | TURNER | 1800 | 7698 |
    |     30 | WARD   | 1550 | 7698 |
    |     30 | SCLARK | 1750 | 7839 |
    +--------+--------+------+------+
    15 rows in set
    

    Example of an aggregate function

    Return the position of 3300 in the sal column.

    obclient> SELECT CUME_DIST(3300) WITHIN GROUP (ORDER BY sal) FROM emp_msg;
    +----------------------------------------+
    | CUME_DIST(3300)WITHINGROUP(ORDERBYSAL) |
    +----------------------------------------+
    |                                  .9375 |
    +----------------------------------------+
    1 row in set
    

    Example of an analytic function

    Group the data records in the table by the deptno column and sort the data records by the sal column in descending order. Return the position of each value in the sal column.

    obclient> SELECT deptno,ename,sal,
                  CUME_DIST ( ) over (PARTITION BY deptno ORDER BY sal DESC ) "RANK"
              FROM emp_msg;
    +--------+--------+------+-------------------------------------------+
    | DEPTNO | ENAME  | SAL  | RANK                                      |
    +--------+--------+------+-------------------------------------------+
    |     10 | KING   | 5300 | .3333333333333333333333333333333333333333 |
    |     10 | CLARK  | 2750 | .6666666666666666666666666666666666666667 |
    |     10 | MILLER | 1600 |                                         1 |
    |     20 | FORD   | 3300 |                                        .4 |
    |     20 | SCOTT  | 3300 |                                        .4 |
    |     20 | JONES  | 3275 |                                        .6 |
    |     20 | ADAMS  | 1400 |                                        .8 |
    |     20 | SMITH  | 1100 |                                         1 |
    |     30 | BLAKE  | 3150 | .1428571428571428571428571428571428571429 |
    |     30 | ALLEN  | 1900 | .2857142857142857142857142857142857142857 |
    |     30 | TURNER | 1800 | .4285714285714285714285714285714285714286 |
    |     30 | SCLARK | 1750 | .5714285714285714285714285714285714285714 |
    |     30 | MARTIN | 1550 | .8571428571428571428571428571428571428571 |
    |     30 | WARD   | 1550 | .8571428571428571428571428571428571428571 |
    |     30 | JAMES  | 1250 |                                         1 |
    +--------+--------+------+-------------------------------------------+
    15 rows in set
    

    Previous topic

    COVAR_SAMP
    Last

    Next topic

    DENSE_RANK
    Next
    What is on this page
    Purpose
    Syntax
    Parameters
    Return type
    Examples
    Example of an aggregate function
    Example of an analytic function