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

Extracting data of recent n active days of every user from dataframe of gaming activity

So, first of all, data looks like this : Sample Data if you would like to work Every record in the table represents a specific user playing a specific type of game with some amount of money. So, it is possible that a user might play one day and not…
1
vote
1 answer

spark window function missing values

i have a dataframe +------+---------------+--------------+-------------------+ |devId |servertimestamp|trackingnumber| servertimestamp2| +------+---------------+--------------+-------------------+ | 8010| 1602022571| 480027192318|2020-10-06…
rigby
  • 1,280
  • 3
  • 13
  • 21
1
vote
1 answer

Is there a Count function within the aggregate Window functions in pyspark?

I´m trying to count the number of rows within a window and having that value in a column for each window. To do that, I´m just using the row_number function, and then getting the max number of that row_number column. My question is: Is there a more…
Nico Arbar
  • 166
  • 1
  • 8
1
vote
2 answers

Getting error in MySQL as Error Code: 1054. Unknown column 'Price' in 'having clause'

I have created a table as follows: create table products (productID int not null auto_increment, Name varchar(30), Price float , CoffeOrigin varchar(30), primary key (productID)); Now the question is "Display the name of products whose price…
1
vote
2 answers

Creating required SQL script

My next question is about creating SQL script as required. Here is my wish: I want to select ID and DOC columns from my TEST1 table where the difference(subtraction) between two consecutive operations over ID column is less than three minutes. Here…
Rahid Zeynalov
  • 172
  • 1
  • 10
1
vote
1 answer

How to get a continuous group of id

I'd like to increment an id by group, for each variation of an attribute with only 2 values. like this : 1 banana 2 banana 3 egg 4 egg 5 banana 6 egg 7 egg result wanted id food grp 1 banana 1 2 banana 1 3 egg …
1
vote
1 answer

Consider table has 4 user login-logout records for a day. I want to determine the login and logout separately

I have below data Emp. Log data A1. 20-JAN-2020 13:15:00 A1. 20-JAN-2020 12:00:00 A1. 20-JAN-2020 10:50:00 A1. 20-JAN-2020 07:00:00 From above, I want to derive login logout data. So 0700 AM is the first login, then lead time is the…
rani james
  • 23
  • 4
1
vote
1 answer

Oracle PLSQL update query for eliminating duplicate entries using sequence

I have a column ref_key in my table events in oracle database (plsql) where ref_key i am generating from creation_time in format update events set ref_key= 'EV_'|| TOCHAR(creation_time, 'YYMMDD_HH24MISS'); now the problems is events are generated…
SRana
  • 125
  • 1
  • 12
1
vote
3 answers

Month over Month percent change in user registrations

I am trying to write a query to find month over month percent change in user registration. \ Users table has the logs for user registrations user_id - pk, integer created_at - account created date, varchar activated_at - account activated date,…
user11866342
1
vote
2 answers

Spark SQL - Finding the maximum value of a month per year

I have created a data frame which contains Year, Month, and the occurrence of incidents (count). I want to find the month of each year had the most incident using spark SQL.
1
vote
1 answer

MySQL with counters

My query: SELECT name FROM users It's possible add a counter for each differente name. For example: mark 1 john 1 mark 2 louis 1 john 2 Ann 1 Thank you
user2671169
  • 203
  • 1
  • 10
1
vote
2 answers

How to get ranking using sub query in Laravel?

Version Laravel : 7.28.3 mysql : Ver 14.14 Distrib 5.7.29, for osx10.15 (x86_64) using EditLine wrapper Tables contents (id) content_views (id, content_id) What I'm Trying To Do I would like to get a rank of a content by how many it has…
1
vote
1 answer

Cumulative balance across two tables

I want to get data from 2 tables ordering them by date: to get the cumulative balance for the customer The 2 tables that I want to get data from are my tables: transfers & trans_payments transfers: ID int cust_id int tfrom …
Rabeea qabaha
  • 526
  • 8
  • 23
1
vote
1 answer

How to convert MySQL rows to a column of 5

I'm new to MySQL. I have a datatable and I'll like to convert it to a row of 5. From SELECT File FROM tbl_file. I have +--------+ | File | +--------+ | B.jpg | +--------+ | X.jpg | +--------+ | H.png | +--------+ | C.png | +--------+ |…
Tyler Wayne
  • 287
  • 4
  • 11
1
vote
2 answers

Select a row in a SQL table only if a value is in the next few rows of the table?

How do I select rows from a SQL table with a specific column value, but only if one of two specific column values are in the next five rows? For example I have a table that looks like the following: id | name | action ----------------------- 1…
Ross
  • 15
  • 2