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

Window function ignores order if there is unused named window in the query

CREATE TABLE tablename ( `SchoolName` VARCHAR(10), `Marks` INTEGER ); INSERT INTO tablename (`SchoolName`, `Marks`) VALUES ('A', 71), ('A', 71), ('A', 71), ('B', 254), ('B', 135), ('B', 453), ('B', 153); SELECT * FROM (…
Nick
  • 11
  • 3
1
vote
2 answers

SELECT user ranking on MySQL by an offset

I'm trying to make a query in MySQL that returns me only 10 users from the table, but with a rank value that is the ORDER result of the xp column. Right now I have this: SELECT id, xp, @curRank := @curRank + 1 AS rank FROM usuarios, (SELECT…
Eleiber
  • 62
  • 6
1
vote
1 answer

SQL dynamic running total

I'm hoping somebody can assist me with a query, I need to build a running total that goes up and down, this is rough idea of the data set poolname date status test1 2018-11-01 08:39:09.737 started test1 2018-11-01…
Yen
  • 23
  • 1
  • 4
1
vote
1 answer

SQL Shifting 2 columns a row down with rollover

I am currently trying to manipulate a table in SQL Server. The following table is an example: Id | PrimaryId | PrimaryMail | SecondaryId | SecondaryMail 1. 1. email1@something. 5. email2@something 2. 2. …
Guyke
  • 21
  • 2
1
vote
1 answer

Postgresql - min/max date range within group

We have a transactional table that stores the data much like a historical table where any time a status changes (or other attributes) it gets effective dated. Example: Product | Status | Start Date | End Date ----------+-------…
1
vote
3 answers

How to incrementally count the number of repeated instances

I want to incrementally count the number of repeated instances in a table, so if I have a table like this: id | name | status | ----------------------- 1 | John | 1 | 2 | Jane | 1 | 4 | John | 1 | 5 | John | 1 …
1
vote
3 answers

how to get percentage out of 100 for salary

Im using sql developer and i want to: add total of salary to the last row percentage in a new column, it should shows the percentage for each an employee out of 100% How to do it ? select name ,sal from( select last_name name,salary sal from…
kkk
  • 19
  • 3
1
vote
1 answer

Take Sum of time difference and Last value of a column

I have a table in which we store the StartTime and StopTime for a task. One task can be assigned to multiple technicians. One task can have multiple start times, stop times (so multiple rows, here Doc Num is primary ID). I want to calculate Sum of…
Rengaldo
  • 31
  • 1
  • 1
  • 6
1
vote
2 answers

SQL FIFO inventory query for aging a dynamic number of units

I have the below table of inventory transactions that I would like to use to identify the age of units I sell. I would like a script that will allow me to input a number of units to buy, and the result spit out which units will be selected based on…
warrenk
  • 119
  • 1
  • 7
1
vote
2 answers

Select row in group with largest value in particular column postgres

I have a database table which looks like this. id account_id action time_point 3 234 delete 100 1 656 create 600 1 4435 update …
1
vote
1 answer

End date Manipulation

I wanted to manipulate the end date to next record startdate or records that happened on same day for shopID and then grab the starttime of later record and update the value in the prior row endtime if the starttime prior to next row start time and…
John
  • 47
  • 2
1
vote
3 answers

MYSQL - get first record based on column value

I have the following employee_sequence table | id | employee_id | sequence_id | is_completed | |----|:-----------:|:-----------:|:------------:| | 1 | 12 | 3 | 1 | | 2 | 12 | 4 | 1 | | 3 …
1
vote
2 answers

sql find high using window function

I have a table like this +------+------+-------+--+ | Name | Date | Price | | +------+------+-------+--+ | x | d1 | 50 | | | x | d2 | 45 | | | x | d3 | 55 | | | x | d4 | 40 | | | x | d5 | 48 | | | x |…
Shh
  • 986
  • 9
  • 18
1
vote
1 answer

rolling function with variable width R

I need to summarize some data using a rolling window of different width and shift. In particular I need to apply a function (eg. sum) over some values recorded on different intervals. Here an example of a data frame: df <- tibble(days =…
Marco De Virgilis
  • 982
  • 1
  • 9
  • 29
1
vote
1 answer

Need to group values in SQL

Below is my table data structure. select 100 id, 1 srno,0 amt from dual union all select 100 id, 2 srno, 1000 amt from dual union all select 100 id, 3 srno, 1000 amt from dual union all select 100 id, 4 srno, 0 amt from…