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

Getting the next TimeStamped Result to tie to the prior result

My table looks like this in BigQuery +--------------------+------------+--------+--------+ | TimeStamp | Session | Type | Result | +--------------------+------------+--------+--------+ | 7/28/2020 16:05:02 | 1595952288 | Select | …
scarebear
  • 157
  • 7
1
vote
1 answer

How to sumarize rows in Oracle?

I have a question that is more like a way to find a idea to how to solve my problem. So the problem: Every 2 minutes I receive a new line at my table with various information. Are they: ID_VEIC - Vehicle ID DT_POSI - Position date LAT -…
1
vote
1 answer

How to select multiple columns and group by one column

I want to sum C.AMOUNT based on the number column. For example the number column in the same 4 numbers, then I want to sum the column C.AMOUNT based on the same number in the number column and display the summed column in the FINAL_AMOUNT column …
1
vote
3 answers

SQL - Create number of categories based on pre-defined number of splits

I am using BigQuery, and trying to assign categorical values to each of my records, based on the number of 'splits' assigned to it. The table has a cumulative count of records, grouped at the STR level - i.e., if there are 4 SKUs at 2 STR, the SKUs…
hstein5
  • 11
  • 2
1
vote
3 answers

How to transform a range of records to the values of the record after that range in SQL?

I am trying to replace some bad input records within a specific date range with correct records. However, I'm not sure if there is an efficient way to do so. Therefore my question is how to transform a (static) range of records to the values of the…
Yoorizz
  • 217
  • 2
  • 12
1
vote
1 answer

query first available slot postgres

I have a table called chest chest_id integer NOT NULL index integer NOT NULL I can get the next index by querying select max(index) + 1 from chest group by chest_id In case there is some index in the order that is not filled, how to get it? for…
Leonardo da Silva
  • 1,285
  • 2
  • 10
  • 25
1
vote
0 answers

Correlated Subquery and Windowed Function

I have been playing with an e-commerce sample database I migrated from SQL Server to GraphDB. I have been translating SQL queries to SPARQL and have a few of them already. However, I'm finding difficult to do things like calculating running totals,…
M Barbieri
  • 21
  • 1
1
vote
1 answer

Ranking by Groups in SQL SERVER

I need to rank the following groups by 'String' and 'Is_sep' and keep the order by 'Id': (SQL Server) ID | String | Is_Sep | Rank | Rank_DESC 1 | XX | 0 | 1 | 3 2 | XX | 0 | 2 …
1
vote
3 answers

Adding rounded decimal percentages to equal 1 (or 100%)

I have a requirement to calculate percentage of a series of numbers balances in this case, rounded to 10 decimal places, but they must total 1.0000000000 (or 100.0000000000%). I can get 0.9999999998 or 1.0000000004, but I cannot seem to get it to…
jc_dollar
  • 11
  • 3
1
vote
2 answers

Count distinct per month/year but display all dates in the query result

DB-Fiddle CREATE TABLE sales ( id int auto_increment primary key, orderID VARCHAR(255), sent_date DATE ); INSERT INTO sales (orderID, sent_date ) VALUES ("Order_01", "2019-03-15"), ("Order_01", "2019-03-16"), ("Order_02",…
Michi
  • 4,663
  • 6
  • 33
  • 83
1
vote
1 answer

Subtract constant across database tables

I need to subtract a value, found in a different table, from values across different rows. For example, the tables I have are: ProductID | Warehouse | Locator | qtyOnHand ------------------------------------------- 100 | A | 123 |…
RozzaT
  • 13
  • 2
1
vote
4 answers

How find TOP/MAX value for each id via SQL query in Oracle?

How do I use a query to find the highest value for each identifier (not unique)? My table: id date repeat_cycle 8 30.07.2020 0 4 28.04.2020 1 4 28.04.2020 0 15 01.01.2020 9 15 24.12.2019 8 15 …
Cesc
  • 274
  • 2
  • 14
1
vote
1 answer

How to get sum of values in a column using OVER() function?

So I have a table that has stores information per student, their date of birth and the start date of the school. The output needed is % of students grouped by their age The query in Biqquery is written as follows, Select…
1
vote
4 answers

Query previous rows on a condition

I have a table of data about a user's flight booking patterns on a website. Let's assume the following data is all the historical data I have about my user. The session_date is the day that the user came onto the website and searched a specific…
1
vote
1 answer

Oracle SQL - trying to calculate running total with Group By without having an existing numerical column to sum

I'm wondering if anyone can help me. I've got the following table structure, and I'm trying to get a running total of the count of products, grouped by date and product, i.e. for each distinct date in Date_Ordered, I want each distinct Product…
1 2 3
99
100