Tabula Documentation
Tabula HomeCommunity
  • 👋Introduction to Tabula
  • Getting Started
    • Product Updates
    • Getting Started
      • Installation and Login
      • Beginner's Guide
    • FAQ
  • Product overview
    • Home Page
    • Exploring Data
      • Data Catalog
      • Exploring Datasets
      • Statistics Panel
    • Designing Flows
      • Creating Flows
      • Flow Designer Guide
        • Working with Canvas
        • Using Groups
        • Working with Table
      • Managing Flows
      • Sharing Flows
      • Demo: Building a Simple Flow
    • Executing Flows
      • Running Flows
      • Jobs overview
    • Building Reports
      • Designing Reports
      • Running Reports
      • Reports Page
    • Connecting Data
  • Integrations
    • Enrichments
      • How to add your API key in Tabula
      • List of Supported Queries
      • Enrichment Providers
        • AnymailFinder
        • Apollo
          • How to find Apollo API key
          • Enrich person by LinkedIn
          • Enrich company by domain
        • Bounceban
        • Bouncer
        • Bouncify
        • CaptainVerify
        • Cleanify
        • Clearout
        • CompanyEnrich
        • ContactOut
          • How to find ContactOut API key
          • Enrich person by LinkedIn
          • Enrich person by email
        • Discolike
        • TheCompaniesAPI
        • Findymail
        • Emailable
        • EmailListVerify
        • Enrichley
        • Heybounce
        • Hunter
        • Kickbox
        • Mails
        • MailChecker
        • MillionVerifier
        • NeverBounce
        • Nubela (Proxycurl)
        • PeopleDataLabs
        • Prospeo
        • ZeroBounce
        • ReverseContact
          • How to find Reverse Contact API key
          • Enrich person by LinkedIn
          • Enrich person and company by email
          • Enrich company by domain
          • Enrich company by LinkedIn
        • UpLead
    • Data Sources
      • Configuring Fivetran Integration
    • Data Storages
      • PostgreSQL
      • Snowflake
      • BigQuery
      • ClickHouse
  • Data Transformation
    • Transforms
      • Source
      • New Empty Table
      • Output
      • Chart
      • Enrichment
      • New Column
      • If...Then
      • Rolling Functions
      • Column Type
      • Columns Edit
      • Filter
      • Remove Duplicates
      • Sort
      • Find and Replace Text
      • Split Column
      • Extract Text
      • Match Text
      • Join
      • Union
      • Group By
      • Pivot
      • Unpivot
      • To JSON
      • From JSON
      • API Call
      • AI Column
      • AI Table
    • Formulas
      • What are Formulas?
      • Math Functions
        • Abs
        • Ceiling
        • Exp
        • Floor
        • IsEven
        • IsOdd
        • Ln
        • Log
        • Log10
        • Mod
        • Pi
        • Power
        • Quotient
        • Round
        • RoundDown
        • RoundUp
        • Sign
        • Sqrt
        • Truncate
      • Trigonometric Functions
        • Acos
        • Asin
        • Atan
        • Atan2
        • Cos
        • Cot
        • Degrees
        • Radians
        • Sin
        • Tan
      • String Functions
        • Compare
        • Concat
        • Contains
        • In
        • CountMatches
        • CountMatchesRegexp
        • EndsWith
        • EndsWithRegexp
        • Extract
        • FindMatchOfString
        • FindMatchOfRegexp
        • FindMatchesOfString
        • FindMatchesOfRegexp
        • Left
        • Length
        • Lower
        • Matches
        • Pad
        • ProperCase
        • RemoveSymbols
        • RemoveWhitespaces
        • Repeat
        • Replace
        • ReplaceRegexp
        • Reverse
        • Right
        • Spaces
        • Split
        • SplitRegexp
        • StartsWith
        • StartsWithRegexp
        • Stuff
        • Substring
        • SubstringDelimiter
        • SubstringRegexpDelimiter
        • Trim
        • Upper
      • Date & Time Functions
        • Date
        • DateAdd
        • DateAdd2
        • DateDiff
        • DateDiff2
        • DateFromParts
        • DateTime
        • DateTimeFromParts
        • DateTrunc
        • DayName
        • DayOfMonth
        • DayOfWeek
        • DayOfYear
        • Hour
        • Minute
        • Month
        • MonthName
        • Now
        • Quarter
        • Second
        • Time
        • TimeFromParts
        • Today
        • Week
        • Year
      • Aggregate Functions
        • Any
        • AnyIf
        • Array
        • ArrayIf
        • Avg
        • AvgIf
        • AvgInRow
        • Count
        • CountA
        • CountIf
        • CountUnique
        • Max
        • MaxIf
        • MaxInRow
        • Median
        • MedianIf
        • Min
        • MinIf
        • MinInRow
        • Mode
        • ModeIf
        • Percentile
        • Quartile
        • StdDev
        • StdDevIf
        • Sum
        • SumIf
        • SumProduct
        • Variance
        • VarianceIf
      • Conversion Functions
        • ToArray
        • ArrayToString
        • ToBoolean
        • ToDate
        • ToDateTime
        • ToDecimal
        • ToInteger
        • ToObject
        • ToTime
        • ToString
      • Misc Functions
        • At
        • IsMissing
        • RowNumber
        • Random
        • If
        • Coalesce
        • True
        • False
        • Null
        • $target
      • Window Functions
      • Custom Functions
      • Data Types
      • Supported Date Parts
      • Regex: List of Tokes
  • Pricing & Billing
    • Plans, Subscriptions, and Credits
    • Tabula for Education
  • Tutorials
    • Tabula Use Cases
    • Merge Columns
    • Join Types
    • Union Introduction
    • Window Functions
    • What is Unpivot?
    • JSON Format Tutorial
    • Using Regex
Powered by GitBook
On this page
  • Introduction
  • Example

Was this helpful?

  1. Tutorials

Window Functions

Calculates a window function operates on a group (“window”) of related rows

Introduction

What are Window Functions?

Window functions are a type of analytical function in SQL and other programming languages that perform calculations across a set of rows related to the current row. Unlike regular aggregate functions, which return a single value for a group of rows, window functions return a value for each row, based on the rows within its "window."

Applications of Window Functions

Window functions are commonly used in various applications, such as:

  • Running totals

  • Cumulative sums or averages

  • Moving averages

  • Ranking or numbering rows

  • Calculating percentiles

Types of Window Functions

There are several types of window functions available in SQL and other programming languages:

  • Ranking functions: ROW_NUMBER()

  • Aggregate functions: SUM(), AVG(), MIN(), MAX(), and COUNT()

Parameters in Window Functions

Window functions can be configured with several parameters that determine the range, ordering, and partitioning of the data within the window. Here, we'll discuss the key parameters and their usage in window functions.

  1. PARTITION BY: This parameter is used to divide the data into partitions to which the window function is applied. If you don't specify the PARTITION BY clause, the function will treat the whole result set as a single partition.

  2. ORDER BY: This parameter is used to specify the order in which the rows will be processed by the window function. It's important to define the order, especially when using ranking or numbering functions, as it directly impacts the output.

  3. ROWS BETWEEN: This parameter is used to define the range of rows that should be included in the window frame, relative to the current row. It allows you to specify the frame using one of the following options:

    • UNBOUNDED PRECEDING: Includes all rows from the start of the partition to the current row.

    • UNBOUNDED FOLLOWING: Includes all rows from the current row to the end of the partition.

    • n PRECEDING: Includes the previous n rows before the current row.

    • n FOLLOWING: Includes the next n rows after the current row.

    • CURRENT ROW: Only includes the current row.

Example

In this example, we want to calculate the running total of sales for each product, ordered by the date.

Consider the following sales table:

id
date
product_id
amount

1

2023-01-01

1

100

2

2023-01-02

1

150

3

2023-01-03

2

75

4

2023-01-04

2

200

5

2023-01-05

1

50

6

2023-01-06

2

300

Parameters

SUM(amount)

PARTITION BY product_id

ORDER BY date

The result would be:

id
date
product_id
amount
running_total

1

2023-01-01

1

100

100

2

2023-01-02

1

150

250

5

2023-01-05

1

50

300

3

2023-01-03

2

75

75

4

2023-01-04

2

200

275

6

2023-01-06

2

300

575

In this example, the SUM() window function is used and includes both the PARTITION BY and ORDER BY clauses. The PARTITION BY product_id clause ensures that the running total is calculated separately for each product, while the ORDER BY date clause sorts the rows within each partition by date.

Last updated 1 year ago

Was this helpful?