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
1
vote
3 answers

SQL ROW_NUMBER OVER syntax

I have this: SELECT ROW_NUMBER() OVER (ORDER BY vwmain.ch) as RowNumber, vwmain.vehicleref,vwmain.capid, vwmain.manufacturer,vwmain.model,vwmain.derivative, vwmain.isspecial, vwmain.created,vwmain.updated,vwmain.stocklevel, …
Ben Durkin
  • 389
  • 1
  • 4
  • 18
1
vote
1 answer

How to select a particular partition in Postgres when using partition by clause

I have a query with the following structure - select a.a1,b.b1,c.c1,d.d1, count(e.e1) over (partition by e.e2) from a join b on a.aid = b.bid join c on b.bid = c.cid join e ......many other joins; The problem is that I want to do…
Kumar Vaibhav
  • 2,412
  • 8
  • 27
  • 50
1
vote
2 answers

How to sum up with SQL values in previous rows in SELECT query in Access?

I have a table named "new_meetings": (ID_to, meetdate, amount) ------------------------- (0, 01.07.2004, 3) (0, 14.07.2004, 1) (0, 11.02.2008, 5) (1, 11.02.2008, 2) (1, 12.02.2008, 1) So I need and sql query, that will give me the result: (0,…
user3824666
  • 233
  • 1
  • 3
  • 10
1
vote
1 answer

Subquery with the same WHERE clause as query

In Postgres 9.4, I have a table which contains user's emails sent by different senders. I need to generate the list of the recent distinct senders, I'm using following query: SELECT DISTINCT ON (tableA.senderName) …
1
vote
1 answer

When Using OVER with COUNT, What Does It Mean to Use Two Arguments With PARTITION BY?

SELECT M.Listing_ID, COUNT(1) OVER (PARTITION BY M.User_ID,EXTRACT(MONTH FROM M.Start_Date) ORDER BY M.Start_Date, M.Listing_ID ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) X FROM LISTINGS M Here is…
1
vote
1 answer

First two rows per combination of two columns

Given a table like this in PostgreSQL: Messages message_id | creating_user_id | receiving_user_id | created_utc -----------+------------------+-------------------+------------- 1 | 1 | 2 | 1424816011 2 …
Mike V
  • 5,136
  • 6
  • 23
  • 34
1
vote
1 answer

Count string values across moving window

I'm trying to count the occurrence of string values across a moving window. Specifically I want to count the occurrences of each string value for the previous 3 rows -- excluding the row My data looks something like this: id | color ---+--------- …
Ellis Valentiner
  • 1,806
  • 3
  • 19
  • 32
1
vote
1 answer

PostgreSQL: count number of occurrences of a value in a column using the last entry per day, per name

I have a table which looks like this: id | name | time | measurement ---+----------+----------------------+---------------- 10 | abc | 2015-02-13 20:12:34 | 2 9 | abc | 2015-02-13 19:12:34 | 1 8 | …
Nikolay Yordanov
  • 1,254
  • 13
  • 24
1
vote
2 answers

Top 3 and the sum of the rest in Oracle SQL

Suppose I get the following result for a GROUP BY query on a table: Name Count(*) Apple 6 Mango 3 Grape 8 Pomegranate 1 Strawberry 13 How can I get the top three elements listed and the rest of the elements summed up…
RJ Doe
  • 49
  • 1
  • 4
1
vote
1 answer

Rows to Columns with LEAD/LAG function

I would like to get help to achive a specific result with Oracle 11gR2. First of all, I need to start with Table "RAW_DATA" arranged like this: CREATE TABLE RAW_DATA AS SELECT 'MTL' AS EMH_CED,'ATW 25-55' AS EMH_ID,to_date('2014-12-03…
JGLord
  • 67
  • 4
1
vote
1 answer

Insert data into empty cells in ascending order

I have a Postgres table with following structure: CREATE TABLE tb1 ( id integer, name text, date date, time time without tz ); CREATE TABLE tb2 ( id integer, name text, date date ); I need to generate a 3rd table tb3 that will have column time_now…
1
vote
2 answers

Given time/interval to calculate open/high/low/close value in each grouped data

Suppose raw data is: Timestamp High Low Volume 10:24.22345 100 99 10 10:24.23345 110 97 20 10:24.33455 97 89 40 10:25.33455 60 40 50 10:25.93455 40 20 60 With a sample time of 1 second, the output data should be as…
newBike
  • 12,989
  • 25
  • 88
  • 158
1
vote
2 answers

Select first & last date in window

I'm trying to select first & last date in window based on month & year of date supplied. Here is example data: F.rates | id | c_id | date | rate | --------------------------------- | 1 | 1 | 01-01-1991 | 1 | | 1 | 1 | 15-01-1991 |…
Gening D.
  • 253
  • 3
  • 13
1
vote
2 answers

SQL Recursion with row_numbers

I got a temporary view in PostgreSQL which offers the following data (simplified): temporary_view (sum, rw): |sum | rw| |1337 | 1| |42 | 2| |... | ...| Now I need to do a recursion on this until my condition is reached. For…
Frame91
  • 3,317
  • 5
  • 36
  • 88
1
vote
1 answer

Using rank over partition in MSSQL query

I have a table called relationships_split this is the view structure with some data create table relationships_split (rel id int , rel_type_id varchar (20), rel_type_group_id varchar(20), rel_type_class_id varchar(20), contact_id…
SRahmani
  • 240
  • 1
  • 4
  • 16
1 2 3
99
100