Questions tagged [window-functions]

A window function is a type of SQL operation that aggregates over a partition of the result set.

As a basic example of a window function, consider a table that contains a list persons along with their age and gender.
If you wanted a query to return the complete list of people along with their age rank per gender, you could accomplish this most simply by using a ranking window function.
Such a function can be used to rank individual rows within a subset of the results.

Using a window function the above function would look like this:

SELECT name, 
       age, 
       gender, 
       RANK() OVER (PARTITION BY gender ORDER BY age DESC) AS age_rank
FROM persons

Window functions as defined by the SQL standard can also access rows before and after the current row using the lag() or lead() function. The following query calculates the delta to the previous row for the amount column for each customer:

SELECT customer_id,
       order_date,
       amount, 
       amount - lag(amount) over (partition by customer_id ORDER BY order_date) as delta
FROM order
ORDER BY customer_id, order_date;

Standard aggregates (e.g. count(), sum()) can also be used as a window function. If combined with an ORDER BY (inside the partition defintion) this will generate "running" totals based on the aggregate being used.

Window functions are available in a wide range of DBMS: Oracle (called "analytical functions" there), Microsoft SQL Server, DB2 (called "olap functions" there) PostgreSQL, Teradata, Sybase, Vertica, and partially in H2 Database.

4082 questions
1
vote
1 answer

Why this default OVER () behavior for the frame?

I've noticed from the following two window functions: WITH sales AS ( select 2020 as year, 100 as revenue UNION select 2021 as year, 200 as revenue UNION select 2022 as year, 300 as revenue UNION select 2023 as year, 100 as…
samuelbrody1249
  • 4,379
  • 1
  • 15
  • 58
1
vote
2 answers

Return first and last timestamp for each ID in sequence with possible repeated and missing values

I have a list of users, application IDs, and activity timestamps that track the users activity throughout the day. The data is structured such as it shows a line of events for every activity on every application ID: user A will go in to conduct 6…
A. Oli
  • 41
  • 1
  • 6
1
vote
1 answer

postgres sql window function - trying to consolidate array_agg based on date and employee_id

I am currently using postgres and there is this sql window function that is used to generate employee check in and out time based on date. But its flawed. CREATE OR REPLACE VIEW view_test AS SELECT row_number() OVER () AS id, …
Axil
  • 3,606
  • 10
  • 62
  • 136
1
vote
2 answers

Last 6 rows for only one group

i have table: label added 1-1 2020-11-09 08:22:37.000 x1 2020-11-09 07:22:37.000 x3 2020-11-09 07:25:43.000 x6 2020-11-09 05:42:37.000 x1 2020-11-07 04:22:37.000 1-1 2020-11-09 08:22:37.000 1-1 2020-11-09…
Cesc
  • 274
  • 2
  • 14
1
vote
3 answers

How to get adjacent value in an OVER() window

I have the following data and query to get the season with the MAX(wins) up to the current season: WITH results as ( SELECT 'DAL' as team, 2010 as season, 6 as wins union SELECT 'DET' as team, 2010 as season, 6 as wins union SELECT 'DET'…
David542
  • 104,438
  • 178
  • 489
  • 842
1
vote
2 answers

How to nullify result in OVER() if the interval isn't "full"

Let's say I have the following dataset and query: WITH results as ( SELECT 'DAL' as team, 2010 as season, 6 as wins union SELECT 'DET' as team, 2010 as season, 6 as wins union SELECT 'DET' as team, 2011 as season, 10 as wins union …
David542
  • 104,438
  • 178
  • 489
  • 842
1
vote
1 answer

LEAD () Window Function not working in MYSQL Workbench

SELECT x,y,z, LEAD(x) OVER(PARTITION BY y,z ORDER BY x) AS column FROM Table; X here is a timestamp field, This is not working in MYSQL Workbench. I am getting the below error ( is not valid at this position, expecting EOF,';' What is wrong here?…
curious123
  • 21
  • 1
  • 4
1
vote
2 answers

How to get lead value of a specific case in Postgresql?

I have a table like below CREATE TABLE events ( id SERIAL PRIMARY KEY, group_id INT, code VARCHAR, created_date timestamptz ); Sample values INSERT INTO events (group_id, code, created_date) VALUES (1, '0001', clock_timestamp()), (1,…
Balakrishnan
  • 2,403
  • 2
  • 26
  • 47
1
vote
1 answer

How do I set up a rolling 7 day 75th percentile in SQL?

I have a table that has the following columns: Event Date Location Employee Id Task Name Volume Per Hour Using PostgreSQL, I need to calculate the 75th percentile of Volume Per Hour for a given location and task name across all employee ids and…
1
vote
2 answers

how to avoid sum(sum()) when writing this postgres query with window functions?

Runnable query example at https://www.db-fiddle.com/f/ssrpQyyajYdZkkkAJBaYUp/0 I have a postgres table of sales; each row has a sale_id, product_id, salesperson, and price. I want to write a query that returns, for each (salesperson, product_id)…
brahn
  • 12,096
  • 11
  • 39
  • 49
1
vote
1 answer

Big Query - Calculate start and end date back to back

I have a problem, which I need some advise, I am required to calculate the number of leave calendar days taken back-to-back on big query. (For eg. 2 leave records taken on 07-01-2020 to 10-01-2020 and 13-01-2020 to 15-01-2020, should return…
Rootie
  • 111
  • 1
  • 1
  • 8
1
vote
2 answers

Find second-largest value within bounded window in SQL Server

SQL question - Does anyone know of a way to select the second-highest value from a bounded window? I have pricing data for a set of geographies/products/weeks. I need to create a new column that, for each row, shows the second-highest price offered…
1
vote
1 answer

SQLITE: get last component update for each distinct component

I have a simple SQLITE db that looks a bit like this ID TID LASTUPDATE UPDATE ============================================ 1 213 2020-09-09 ok 2 416 2019-12-25 ok 3 213 2020-11-10 …
push 22
  • 1,172
  • 3
  • 15
  • 34
1
vote
1 answer

Django query to get sum of previous values

I have the following table of logs that stores operations. An override operation means an absolute value stored, while add and sub operations are variance values. The override operations are like checkpoints. Id operation quantity …
1
vote
2 answers

How do I implement OVER using COUNT() multiple times in an expression?

I have a question about a query I'm writing to solve a problem from LeetCode. Here's the problem: Ads +---------------+---------+ | Column Name | Type | +---------------+---------+ | ad_id | int | | user_id | int | |…
spheroidic
  • 199
  • 7