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.

3029 questions
400
votes
12 answers

SQL Server: Difference between PARTITION BY and GROUP BY

I've been using GROUP BY for all types of aggregate queries over the years. Recently, I've been reverse-engineering some code that uses PARTITION BY to perform aggregations. In reading through all the documentation I can find about PARTITION BY,…
Mike Mooney
  • 11,012
  • 3
  • 34
  • 42
270
votes
6 answers

Oracle "Partition By" Keyword

Can someone please explain what the partition by keyword does and give a simple example of it in action, as well as why one would want to use it? I have a SQL query written by someone else and I'm trying to figure out what it does. An example of…
Alex Beardsley
  • 19,982
  • 13
  • 48
  • 67
196
votes
4 answers

Pandas get topmost n records within each group

Suppose I have pandas DataFrame like this: >>> df = pd.DataFrame({'id':[1,1,1,2,2,2,2,3,4],'value':[1,2,3,1,2,3,4,1,1]}) >>> df id value 0 1 1 1 1 2 2 1 3 3 2 1 4 2 2 5 2 3 6 2 4 7 3 1 8 …
Roman Pekar
  • 92,153
  • 25
  • 168
  • 181
171
votes
10 answers

What's the difference between RANK() and DENSE_RANK() functions in oracle?

What's the difference between RANK() and DENSE_RANK() functions? How to find out nth salary in the following emptbl table? DEPTNO EMPNAME SAL ------------------------------ 10 rrr 10000.00 11 nnn 20000.00 11 mmm …
user1357722
  • 5,598
  • 12
  • 32
  • 41
111
votes
1 answer

Select Row number in postgres

How to select row number in postgres. I tried this: select row_number() over (ORDER BY cgcode_odc_mapping_id)as rownum, cgcode_odc_mapping_id from access_odc.access_odc_mapping_tb order by cgcode_odc_mapping_id and got this…
Maverick
  • 1,672
  • 4
  • 20
  • 33
100
votes
6 answers

PostgreSQL unnest() with element number

When I have a column with separated values, I can use the unnest() function: myTable id | elements ---+------------ 1 |ab,cd,efg,hi 2 |jk,lm,no,pq 3 |rstuv,wxyz select id, unnest(string_to_array(elements, ',')) AS elem from myTable id |…
BartekR
  • 3,507
  • 3
  • 22
  • 32
97
votes
1 answer

Calculating Cumulative Sum in PostgreSQL

I want to find the cumulative or running amount of field and insert it from staging to table. My staging structure is something like this: ea_month id amount ea_year circle_id April 92570 1000 2014 1 April …
82
votes
11 answers

Can I do a max(count(*)) in SQL?

Here's my code: select yr,count(*) from movie join casting on casting.movieid=movie.id join actor on casting.actorid = actor.id where actor.name = 'John Travolta' group by yr; Here's the question: Which were the busiest years for 'John Travolta'.…
Alex Gordon
  • 51,480
  • 273
  • 609
  • 976
74
votes
4 answers

OVER clause in Oracle

What is the meaning of the OVER clause in Oracle?
paweloque
  • 17,185
  • 25
  • 75
  • 131
68
votes
5 answers

Best way to get result count before LIMIT was applied

When paging through data that comes from a DB, you need to know how many pages there will be to render the page jump controls. Currently I do that by running the query twice, once wrapped in a count() to determine the total results, and a second…
EvilPuppetMaster
  • 7,052
  • 9
  • 31
  • 30
58
votes
8 answers

Why no windowed functions in where clauses?

Title says it all, why can't I use a windowed function in a where clause in SQL Server? This query makes perfect sense: select id, sales_person_id, product_type, product_id, sale_amount from Sales_Log where 1 = row_number() over(partition by…
Chris Pfohl
  • 15,337
  • 8
  • 60
  • 104
54
votes
2 answers

What is ROWS UNBOUNDED PRECEDING used for in Teradata?

I am just starting on Teradata and I have come across an Ordered Analytical Function called "Rows unbounded preceding" in Teradata. I tried several sites to learn about the function but all of them uses a complicated example explaining the same.…
StrugglingCoder
  • 4,231
  • 11
  • 55
  • 90
51
votes
4 answers

ROW_NUMBER Without ORDER BY

I've to add row number in my existing query so that I can track how much data has been added into Redis. If my query failed so I can start from that row no which is updated in other table. Query to get data start after 1000 row from table SELECT *…
lucy
  • 2,650
  • 4
  • 17
  • 36
43
votes
1 answer

Using window functions in an update statement

I have a large PostgreSQL table which I access through Django. Because Django's ORM does not support window functions, I need to bake the results of a window function into the table as a regular column. I want to do something like this: UPDATE …
jl6
  • 4,988
  • 7
  • 28
  • 59
42
votes
1 answer

PostgreSQL: running count of rows for a query 'by minute'

I need to query for each minute the total count of rows up to that minute. The best I could achieve so far doesn't do the trick. It returns count per minute, not the total count up to each minute: SELECT COUNT(id) AS count , EXTRACT(hour from…
GabiMe
  • 16,471
  • 25
  • 71
  • 106
1
2 3
99 100