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

Create Missing Data Hive SQL

I have a table that has an activity date of when things change such as 2020-08-13 123 Upgrade 2020-08-17 123 Downgrade 2020-08-21 123 Upgrade Basically this in relation to a line there are 3 activities happening on this account. They have a…
enavuio
  • 1,428
  • 2
  • 19
  • 31
1
vote
1 answer

PostgreSQL: Select rows greater than a value by a number of consecutive times

I have the following table: id updated_on ch1 1 2020-03-23 08:30:25 90.577 2 2020-03-23 08:30:55 99.213 3 2020-03-23 08:31:05 101.426 4 2020-03-23 08:31:15 103.457 5 2020-03-23 08:31:25 103.982 6 2020-03-23 08:31:35 …
1
vote
1 answer

Return data where the running total of amounts 30 days from another row is greater than or equal to the amount for that row

Let's say I a table that contains the date, account, product, purchase type, and amount like below: Looking at this table, you can see that for any particular account/product combination, there are buys and sells. Essentially, what I'd like to…
nunga
  • 31
  • 5
1
vote
2 answers

Oracle SQL row counter with restarting incremental

I would like to create a row counter with Oracle SQL. This row counter should start from 1 till 5 and then should restart again from 1. Like: ORDER, ROW_COUNTER ----- …
1
vote
4 answers

Converting this SQL query to use a window function, or any other more efficient method

I have a HUGE table that contains details about all customers, such as their phone numbers and email addresses. The table can have multiple rows for each customer, indicating that at some point they have changed their phone number, email address, or…
KOB
  • 4,084
  • 9
  • 44
  • 88
1
vote
1 answer

Using COUNTIF inside aggregation function

Im making a betting system app. I need to count the points of a user based on his bets, knowing that some of the bets can be 'combined', which makes the calcul a bit more complex than a simple addition. So if i have 3 bets: {points: 3, combined:…
1
vote
2 answers

SQL - get summary of differences vs previous month

I have a table similar to this one: | id | store | BOMdate | | 1 | A | 01/10/2018 | | 1 | B | 01/10/2018 | | 1 | C | 01/10/2018 | |... | ... | ... | | 1 | A | 01/11/2018 | | 1 | C | 01/11/2018 | | 1 | D |…
1
vote
3 answers

How to delete duplicate records (SQL) when there is no identification column?

It is a datawarehouse project in which I load a table in which each column refers to another table. The problem is that due to an error in the process, many duplicate records were loaded (approximately 13,000) but they do not have a unique…
Lucho
  • 51
  • 1
  • 6
1
vote
2 answers

How to ORDER BY based on condition? SQL Server

I have a table below for example: ID NAME Company StartDate EndDate Status -------------------------------------------------------------------------------------- 50 Bob ABC …
1
vote
2 answers

MySQL ( v5.7.30 ) query AVG of AVGs

So I have these 2 tables: jobs: ------------------------------------------------- id business_id other_columns ------------------------------------------------- 1 223 …
xyboox
  • 13
  • 4
1
vote
1 answer

Generate the SQL row numbers based on the 10 days filters

Customer_Id Call_Date Agent_M_Code Row_Indicator 810471698 2020-03-19 13:25:24.910 rmanzan2 1 810471698 2020-03-22 20:28:19.067 pmaclair 2 810471698 2020-03-24 09:22:47.833 …
1
vote
1 answer

Merge timesheet rows with no gap between start and finish times

I'm working on a timesheeting system where employees work on various jobs throughout the day. I need to do a query of some sort that returns a merged version of the times so that if an employee works a full day without any breaks between jobs then I…
1
vote
1 answer

Find user who has given the lowest average rating in '%Y-%m'

There are three tables: Movies, Users, and Rating having the below structure: Movies(id int, title text); Users(id int, name text); Rating(movie_id int, user_id int, rating int, created_at date); I'm trying to find the name the user(s) who has…
Saurabh
  • 5,176
  • 4
  • 32
  • 46
1
vote
2 answers

How to capture first row in a grouping and subsequent rows that are each a minimum of 15 days apart?

Assume a given insurance will only pay for the same patient visiting the same doctor once in 15 days. If the patient comes once, twice, or twenty times within those 15 days to the doctor, the doctor will get only one payment. If the patient comes…
RiSt
  • 63
  • 1
  • 8
1
vote
1 answer

Most recent record MS SQL

Need only the most recent record Current Data: RequestID RequestCreateDate VehID DeviceNum ProgramStatus InvID 1 08/12/2018 13:00:00:212 110 20178 Submitted A1 2 08/11/2018 11:12:33:322 …