Yandex.Cloud
  • Services
  • Why Yandex.Cloud
  • Pricing
  • Documentation
  • Contact us
Get started
Yandex Database
  • Getting started
    • Overview
    • Create databases
    • Examples of YQL queries
    • Examples of operations in the YDB CLI
    • Launch a test app
    • Document API
    • Developing in NodeJS through the Document API
  • Step-by-step instructions
    • Database management
    • How to connect to a database
    • Table management
    • Reading and writing data
    • Working with secondary indexes
  • Working with the SDK
  • Concepts
    • Overview
    • Data model and schema
    • Serverless and Dedicated operation modes
    • Data types
    • Transactions
    • Secondary indexes
    • Time to Live (TTL)
    • Terms and definitions
    • Quotas and limits
  • Access management
  • Pricing policy
    • Overview
    • Serverless mode
    • Dedicated mode
  • Recommendations
    • Schema design
    • Partitioning tables
    • Secondary indexes
    • Paginated output
    • Loading large data volumes
    • Using timeouts
  • YDB API and API reference
    • Database limits
    • Handling errors in the API
  • Amazon DynamoDB-compatible HTTP API
    • API reference
      • All methods
      • Actions
        • BatchGetItem
        • BatchWriteItem
        • CreateTable
        • DeleteItem
        • DeleteTable
        • DescribeTable
        • GetItem
        • ListTables
        • PutItem
        • Query
        • Scan
        • TransactGetItems
        • TransactWriteItems
        • UpdateItem
      • Common errors
  • YQL reference guide
    • Overview
    • Data types
      • Simple
      • Optional
      • Containers
      • Special
    • Syntax
      • Unsupported statements
      • For text representation of data types
      • Expressions
      • CREATE TABLE
      • DROP TABLE
      • INSERT INTO
      • UPSERT INTO
      • REPLACE INTO
      • UPDATE
      • DELETE
      • SELECT
      • GROUP BY
      • JOIN
      • FLATTEN
      • ACTION
      • DISCARD
      • PRAGMA
      • DECLARE
      • OVER, PARTITION BY, and WINDOW
    • Built-in functions
      • Basic
      • Aggregate
      • Window
      • For lists
      • For dictionaries
      • For JSON
      • For structures
      • For types
    • Preset user-defined functions
      • HyperScan
      • Pcre
      • Pire
      • Re2
      • String
      • Unicode
      • Datetime
      • Url
      • Ip
      • Digest
      • Math
      • Histogram
    • For text representation of data types
  • YQL tutorial
    • Overview
    • Creating a table
    • Adding data to a table
    • Selecting data from all columns
    • Selecting data from specific columns
    • Sorting and filtering
    • Data aggregation
    • Additional selection criteria
    • Joining tables by JOIN
    • Data insert and update by REPLACE
    • Data insert and update by UPSERT
    • Data insert by INSERT
    • Data update by UPDATE
    • Deleting data
    • Adding and deleting columns
    • Deleting a table
  • Maintenance
    • Backups
  • Diagnostics
    • System views
  • Questions and answers
    • General questions
    • Errors
    • YQL
    • All questions on the same page
  • Public materials
  1. YQL reference guide
  2. Built-in functions
  3. Window

Window functions in YQL

  • Aggregate functions
  • ROW_NUMBER
  • LAG / LEAD
  • FIRST_VALUE / LAST_VALUE
  • RANK / DENSE_RANK

Aggregate functions

All the aggregate functions can also be used as window functions. In this case, the aggregation result on each row is applied to the current window position (by default, from the start of the partition to the current row, inclusive).

Examples

SELECT
    SUM(int_column) OVER w AS running_total
FROM my_table
WINDOW w AS ();

ROW_NUMBER

Row number inside the partition. No arguments.

Examples

SELECT
    ROW_NUMBER() OVER w AS row_num
FROM my_table
WINDOW w AS ();

LAG / LEAD

Accessing a value from the row that's behind (LAG) or ahead (LEAD) of the current row by a fixed number. The first argument specifies the expression to be accessed, and the second argument specifies the offset in rows. You may omit the offset. By default, the neighbor row is used: the previous or next, respectively (hence, 1 is assumed by default). In the rows that don't have neighbors at the specified distance (for example, LAG(expr, 3) in the first and second rows of the window), the expression value is NULL.

Examples

SELECT
   int_value - LAG(int_value) OVER w AS int_value_diff
FROM my_table
WINDOW w AS ();

FIRST_VALUE / LAST_VALUE

Access values from the first and last rows of the window. The only argument is the expression that you need to access.

Optionally, OVER can be preceded by an additional modifier IGNORE NULLS. It changes the behavior of functions to the first or last non-empty (i.e., non-NULL) value among the window rows. The antonym of this modifier is RESPECT NULLS: it's the default behavior that can be omitted.

Examples

SELECT
   LAST_VALUE(my_column) IGNORE NULLS OVER w
FROM my_table
WINDOW w AS ();

RANK / DENSE_RANK

Number the groups of neighboring rows having the same expression value in the argument. DENSE_RANK numbers the groups one-by-one, and RANK skips (N - 1) values, with N being the number of rows in the previous group.

If there is no argument, it uses the order specified in the ORDER BY section.

Examples

SELECT
   RANK(my_column) OVER w
FROM my_table
WINDOW w AS ();
SELECT
   RANK() OVER w
FROM my_table
WINDOW w AS (ORDER BY my_column);
In this article:
  • Aggregate functions
  • ROW_NUMBER
  • LAG / LEAD
  • FIRST_VALUE / LAST_VALUE
  • RANK / DENSE_RANK
Language
Careers
Privacy policy
Terms of use
© 2021 Yandex.Cloud LLC