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
2 answers

Problems using OVER PARTITION BY

I have some trouble using ROW_NUMBER() OVER (PARTITION BY ...). I have the following table StartTime | EndTime | ContractStart ----------+---------+-------------- 10:00 | 12:00 | 2015-03-02 11:00 | 22:00 | 2014-01-02 10:00 | 12:00 …
ruedi
  • 4,417
  • 11
  • 41
  • 73
1
vote
6 answers

Assigning unique ROW NUMBER() for all the PARTITIONED RECORDS

In this query it is partitioning the records but i need to assign row number for all the result set a row number should not be repeated it should be unique fpr each row.Anyone help ? Thanks in advance. select * from Store order by row_number()…
shalini
  • 15
  • 8
1
vote
1 answer

By Groups in SAS vs window functions in SQL

I have a table, input_table, that contains the following columns: key - double code - string date- string result -string I have the following SAS code: PROC SQL; CREATE TABLE t1 AS SELECT key, code, date, result from input_table …
Danzo
  • 463
  • 3
  • 8
  • 23
1
vote
1 answer

Postgres reporting "window function call requires an OVER clause" in a query that has an OVER clause

I've got a table, me, that's got an e_id column, a first_name, and a last_name. (A few other columns too but I'm just trying to figure out the window function stuff.) When I try to do a query and pick out the first name values in the table for a…
Pointy
  • 371,531
  • 55
  • 528
  • 584
1
vote
1 answer

Calculating TOTAL of certain items using CASE and OVER

I need to calculate the total value of specific product codes. The product codes are 5780 and everything below 5700. To calculate the value I use the code: CASE WHEN [PC].[ProductCategoryCode] = 5870 THEN [ICP].[UnitCost] *…
1
vote
0 answers

PostgreSQL materialized view with global and partitioned ranks

I have a table with 10 mils. of user ratings. I need to create materialized view that has global rating and rating by country and is refreshed once a day. I came up with the following select query: SELECT row_number() OVER(ORDER BY value DESC, id)…
1
vote
2 answers

How to group following rows by not unique value

I have data like this: table1 _____________ id way time 1 1 00:01 2 1 00:02 3 2 00:03 4 2 00:04 5 2 00:05 6 3 00:06 7 3 00:07 8 1 00:08 9 1 00:09 I would like to know in which time interval I was on which way: desired…
1
vote
3 answers

Explanation of sql query : Rank and partition oracle

I am working with oracle database and is stuck at this query, I am not comprehending how this is working, i have removed all the unnecssesary information and the query has come down to this: select RANK () OVER (PARTITION BY A_pk ORDER BY B_pk…
Subham Tripathi
  • 2,513
  • 6
  • 36
  • 65
1
vote
1 answer

OVER() vs Two Queries - Which is Most Efficient

I need to pull back the first 300 rows from a 10MM row table, as well as getting a count of the total number of matching records. I can do this in two queries, something like: SELECT * FROM table WHERE field = value LIMIT 300; SELECT count(*) FROM…
Kong
  • 7,648
  • 14
  • 58
  • 89
1
vote
1 answer

BigQuery: Using threshold with COUNT DISTINCT in WINDOW function returns error

With COUNT DISTINCT, I often make use of a threshold to make it more precise. E.g. COUNT(DISTINCT users, 100000). If I am using a WINDOW function though I get an error when trying to use a threshold COUNT_DISTINCT must have at most 1 argument(s),…
alan
  • 3,717
  • 6
  • 31
  • 48
1
vote
3 answers

ROW_NUMBER over column result

How can I count the number of occurrences in a given column? Follow this example. With a table like this: +----------+---------+------+ | PersonID | Name | City | +----------+---------+------+ | 1 | John | NY | | 2 | Mohit |…
Fábio Silva
  • 129
  • 1
  • 13
1
vote
2 answers

Oracle analytical function suggestions

I have some metrics tracked on a project whose value keeps increasing as time goes by. Sample data: Project | Date | Metric% --------------------------------- P1 | 05-Jan-2015 | 40 P1 | 10-Jan-2015 | 50 P1 | 05-Feb-2015 |…
museshad
  • 402
  • 6
  • 14
1
vote
2 answers

Moving Median, Mode in T-SQL

I am using SQL Server 2012 and I know it is quite simple to calculate moving averages. But what I need is to get the mode and the median for a defined window frame like so (with a window of 2 preceding to current row; month unique): MONTH | CODE |…
Jens
  • 152
  • 8
1
vote
2 answers

Compute a running ratio of two totals

I have a PostgreSQL 9.4.1 database (Retrosheet data) with a table events containing one row per baseball play. I want to a compute a running batting average for a given player: the formula is (total number of hits so far)/(total number of valid…
1
vote
1 answer

SQL Server 2014 LAST_VALUE in GROUP BY

Is it possible to use the LAST_VALUE keyword within a GROUP BY select statement? SELECT colA, LAST_VALUE(colB) OVER (PARTITION BY ... ORDER BY ... ROWS BETWEEN ...) FROM MyTable GROUP BY colA
mils
  • 1,638
  • 2
  • 15
  • 33
1 2 3
99
100