Questions tagged [gaps-and-islands]
Gaps and islands problems involve finding a range of missing values (gaps) or a range of consecutive values (islands) in a sequence of numbers or dates.
1,197
questions
0
votes
1
answer
44
views
Oracle SQL "Queue" Table: Every time an ID leaves the table
I have a "Queue" table in an Oracle sql database where IDs enter on a certain date, and have an entry/row every day they remain in the table until they leave. These IDs can return multiple ...
1
vote
3
answers
54
views
how to get max No between 1 to Next 1 in sql
CREATE TABLE #t1 ( ID int, Furnace_life INT);
INSERT INTO #t1(ID,Furnace_life) VALUES (1,1)
INSERT INTO #t1(ID,Furnace_life) VALUES (2,2)
INSERT INTO #t1(ID,Furnace_life) VALUES (3,3)
INSERT INTO #t1(...
1
vote
1
answer
24
views
Apply LAG function until clause in Redshift
I have the below table (Redshift)
event
url
rank
flag
clic
url1
1
view
url2
2
view
url3
3
✓
clic
url4
1
view
url5
2
✓
I would like to add a column with the url of the last clic. I tried with ...
0
votes
1
answer
29
views
Using MySQL window function to section timeseries when value changes
I am looking for a way to split a dataset into sections whenever a value change inside a partition. Note that each switch of value is a new section - also if the value switches back to the same as in ...
0
votes
2
answers
94
views
Aggregating length of time intervals and grouping to fixed time grid
I have some data consisting of shifts, logging the time periods taken as breaks during the shift.
start_ts end_ts shift_id
2022-01-01T08:31:37Z 2022-01-01T08:58:37Z ...
-1
votes
1
answer
34
views
T-SQL create time windows (from/to) with alternating values
I need to transform rows on a monthly basis to rows with a time window based on alternating values. I have already tried various versions of partition, dense_rank and joins with row_number on ...
2
votes
1
answer
60
views
Generate new UUID per row depending on gaps in existing time series
I have a table1 with timestamps, grouped by an added “Period ID”:
Period ID (uuid) Created At (timestamptz)
A 2017-04-11 11:13:47.997+00
A 2017-04-11 14:42:51....
-1
votes
0
answers
52
views
SQL to select 1 row from group with start date and end date - GAPS and ISLANDS
Here is the SQL server scenario. I wanted to write SQL select statement in sql server latest version for the following scenario
Lets say I have table named 'Demo' like below. It has 3 columns ID, ...
0
votes
0
answers
44
views
Find next available time slot in "calendar"
I have the following MySQL tables:
ServiceProviders- id, ...other irrelevant columns
ProvidersWorkHours - id, providerId, day(enum[1,2,3,4,5,6,7]), startTime(time), endTime(time)
Service- id, ...
0
votes
1
answer
29
views
How to calculate a streak of results in mysql?
I'm completely stuck with this one. I have my matches table,my roster tables and my players table. And I'm using a subquery to determine the result for each player in each match order by the match ...
1
vote
1
answer
50
views
How to copy the previous row value of the same column?
I have several tracked specimens that can move between different areas throughout the day (and its periods). In the example below the calculated field mov has to restart counting whenever there is a ...
0
votes
1
answer
29
views
grouping rows with less than one minute separating row from previous row in Oracle
I have an Oracle table with time stamps and I need to check on all rows where the current row is bigger the the previous row by less than a minute and state the start and end time and if its bigger ...
0
votes
0
answers
29
views
Mysql Find free time periods between appointments and within working periods
Table "Working periods":
id
started_at
ended_at
1
2022-06-09 08:00:00
2022-06-09 12:00:00
2
2022-06-09 13:00:00
2022-06-09 16:00:00
Table "Appointments":
id
started_at
...
0
votes
1
answer
77
views
How to get start and end date and collapse continuous values - ORACLE
Hello I need help please, it seems so simple but I can't seem to get it right...this is currently my output below, I'm looking to make it look nicer by consolidating the continuous values. Filtering ...
-1
votes
1
answer
32
views
can we solve basic transaction query using lead and lag window function in sql
I have this i/p table
balance
trs_date
27000
2020-01-01
27000
2020-01-02
27000
2020-01-03
31000
2020-01-04
31000
2020-01-05
27000
2020-01-06
27000
2020-01-07
32000
2020-01-08
31000
2020-01-...
-1
votes
1
answer
61
views
Count size of islands with no gaps
My data has 3 columns; ID, NAME, VALUE
ID is sequential number like a rowcount
There are many different NAMEs I show 3 in the data but there could be 10+
There are 2 VALUEs
I would like to group the ...
2
votes
2
answers
51
views
Find the next free timestamp not in a table yet
I have a table, event, with a column unique_time of type timestamptz. I need each of the values in unique_time to be unique.
Given a timestamptz input, input_time, I need to find the minimum ...
0
votes
0
answers
28
views
Group up date ranges by interesecting ranges
This is a variation of the gaps and islands problem. I tried finding solutions but they are all SQL based.
Problem: Given a list of date ranges, return a combined list of date ranges where none are ...
1
vote
1
answer
40
views
Continuous subset and ranking using sql
I have a dataset like below:
Now, I need the output as below:
start_time end_time count
10:01 10:04 3
10:05 10:07 2
For this purpose, I wrote a query but it is not giving me the desired ...
6
votes
4
answers
173
views
How to form groups of consecutive dates allowing for a given maximum gap?
Given a table like:
person_id
contact_day
days_last_contact
dash_group
1
2015-02-09
1
1
2015-05-01
81
2
1
2015-05-02
1
2
1
2015-05-03
1
2
1
2015-06-01
29
3
1
2015-08-01
61
4
1
2015-08-04
3
4
...
1
vote
1
answer
45
views
Postgres Show Time Availability
I'd like to ask if there is a built-in query to fetch date time availability in PostgreSQL.
Below are my table:
Room Name
Book Start Date
Book End Date
Room A
09-May-2022 09:00
09-May-2022 11:00
...
2
votes
2
answers
70
views
How to get the biggest value in any consecutive range
Consider the following values in an indexed column of unsigned integers:
1
2
3
3
2
4
6
8
9
For any number provided, I want to get the biggest value in its consecutive range (continue stepping forward ...
0
votes
1
answer
62
views
Self joining a table while maintaining the order of the rows
So i have a table that logs users using certain apps. It has the fields below
id
time
app
user
server
type (can be IN or OUT)
So a user starts using a certain app and when they get the right to use ...
2
votes
1
answer
77
views
How to group data based on continuous date range?
I am trying to group the below sample data based on date and price.
PRODUCT_CODE
PRICING_DATE
PRICE
1234
01-01-2022
23.9
1234
02-01-2022
23.9
1234
03-01-2022
23.9
1234
04-01-2022
22.9
1234
05-01-...
0
votes
3
answers
66
views
calculate each in and out duration for an id
I've data like below
Id
date
time
type
1
01-01-2022
08:00
in
1
01-01-2022
11:30
out
1
01-01-2022
11:35
out
1
01-01-2022
12:45
in
1
01-01-2022
17:30
out
1
01-01-2022
01:00
out
expected output :
...
-2
votes
2
answers
86
views
How do I perform a data smear in SQL
So I have data that is like this:
row EMPREF HOURS
---------------------
1 0023 NULL
2 0023 NULL
3 0023 NULL
4 0023 NULL
5 0023 20
6 0023 ...
1
vote
2
answers
69
views
Combine periods by user
I want to combine periods(start -> end dates) by the intervals they overlap into a single interval by user.
+---------+-------------+------------+------------+
| USER_ID | CONTRACT_ID | START_DATE |...
0
votes
0
answers
42
views
Is there any PostgreSQL magic to get results by range instead of rows? [duplicate]
I'm sorry for the title, I don't know how to clearly summarize the problem.
That's probably why I couldn't find an answer when searching by myself.
Feel free to improve it.
Anyways, let's say I have a ...
2
votes
2
answers
60
views
SQL Cursor to join periods of dates into single row
My goal is to have a single row by id.
Now I have multiple rows like this for each one
For example user number 001 has this:
Ini Date
End Date
Qnty
2023-01-02
2023-01-02
1.0000
2023-01-03
2023-01-...
1
vote
2
answers
43
views
Check date split periods are continuous (and spotting the gaps)
I am struggling with a similar issue like in this thread:
Check date split periods are continuous
Can someone help me translate the Qsebas' answer to Presto language? As this is exactly what I'd like ...
-1
votes
1
answer
105
views
Microsoft SQL Server 2016 - T-SQL puzzle - overlapping date ranges in segregated rows - 'Gaps & Islands' problem
I have asked 'Gaps and Islands' questions in the past, but this one is significantly different. I have an interesting question in Microsoft SQL Server 2016 database, T-SQL language. (Refer to image ...
1
vote
0
answers
43
views
Finding a gap between timestamps (for multiple entries)
I have a table with a list of vehicles and timestamps of adding and deleting them from the database. A single vehicle can be added by more than one user, so there can be more than one row when a ...
-1
votes
2
answers
166
views
SQL query to get start and end date from a result set
I am working on one of requirement the raw data is in following format
Requirement - Startdate should be the date when status changed to 1 and enddate should be the 1st date after the record status ...
0
votes
1
answer
45
views
How to aggregate to group level, order and enumerate groups chronologically? (in SQL Server)
Within a hospital encounter, a patient may be administered several different formulations of a medication, as shown here:
Encounter
Medication
Administration
Adm_Num
1
A
8/31/21 11:33 AM
1
1
B
8/31/...
1
vote
1
answer
638
views
SQL - gap and island issue for more attributes
I have following table, which apart from other attributes contains:
ID - unique identifier
Column1
Column2
Column3
CreatedDate - when the record has been created (based on ETL)
UpdatedDate - until ...
3
votes
2
answers
108
views
Detect flickering in list of durations values
I'm trying to get an average of flickering data that comes from a device that sends me a value periodically.
For instance, it sends me 5 values in a window of 1 minute, then the next value will come ...
1
vote
2
answers
108
views
Combining overlapping date ranges without using a cross join in BigQuery
If I have this dataset:
create schema if not exists dbo;
create table if not exists dbo.player_history(team_id INT, player_id INT, active_from TIMESTAMP, active_to TIMESTAMP);
truncate table dbo....
0
votes
1
answer
40
views
Identifying gaps in a table or set of integers
I would like to identify which IDs have been deleted from a table. For instance, if this is my client table:
client.client_id
1
2
4
6
8
9
I want to discover the values 3, 5, 7.
What I'd like ...
0
votes
1
answer
54
views
Count an island and its start and end
I have a table that looks like this:
What I would like is to count the islands of data based of the Day_Ranking column, so the output would look like something similar to this:
Person Start ...
-1
votes
4
answers
130
views
create time range with 2 columns date_time
The problem I am facing is how to find distinct time periods from multiple time periods with overlap in Teradata ANSI SQL.
For example, the attached tables contain multiple overlapping time periods, ...
0
votes
1
answer
64
views
SQL - Select min and max on grouped columns [duplicate]
I want to select the employees' start and end dates for each department using a SQL query. Taking into mind that an employee might return to a previous department.
As the system creates a new FromDate ...
2
votes
4
answers
79
views
How to check if a group has three consecutive values in a column?
I have a table games with values such as:
+----------+------+
| game | year |
+----------+------+
| Football | 1999 |
| Football | 2000 |
| Football | 2001 |
| Football | 2002 |
| Cricket | 1996 |...
0
votes
1
answer
81
views
Trying to add new column with DENSE_RANK() in sequentially
I'm hoping some examples will help explain the situation.
SELECT
ID,
--ROW_NUMBER() OVER (PARTITION BY CardNumber ORDER BY ID DESC) AS 'RN',
DENSE_RANK() OVER (ORDER BY CardNumber DESC) ...
0
votes
1
answer
43
views
Identifying a gap in member month data based on service date
There's two data sets:
Data set 1:
Member ID
MIN_SRVC_DT
NEW OR CURRENT
111
202104
NEW
222
201801
NEW
333
202002
NEW
Data set 2:
Member ID
COVERAGE_MONTH
111
202102
111
202103
111
202104
111
...
1
vote
2
answers
62
views
Oracle SQL - How to return common date periods and "divide" when there are gaps between periods
I'm trying to return common date periods (per id) from below data, but I cannot find a way to handle case when date periods have a gap between common periods. Can anyone help?
|id|code_id|code|...
1
vote
1
answer
37
views
Creating groupings based on criteria
Hoping to get some assistance with the following gaps and islands problem
I have the following sample dataset:
MEM_ID
CLM_ID
ADM_DT
DCHG_DT
PROV
1
111
01-01-2021
01-01-2021
1
1
112
01-01-2021
02-01-...
-1
votes
3
answers
52
views
How to pick the first and last from a given ordering when partitioning items into groups?
The real problem is with some huge product catalogs. In order to learn how to SQL this properly, I'm testing with this dataset. The jumbled up order of things is intended to help ensure that the logic ...
0
votes
1
answer
26
views
Grouping and min max date assignments based on dates following a unique combination of values
I am having another gaps and islands problem that I am trying to solve.
I have the following sample dataset:
MEM_ID
CLM_ID
BGN_SERV_DT
END_SERV_DT
ADM_DT
DCHG_DT
PROV
1
111
01-01-2021
01-31-2021
01-...
-1
votes
1
answer
55
views
How can I solve gaps and islands problem in mysql for gaps with zero values and islands with non-zero values
I read a lot about MySQL gaps and islands problem, but I cannot find anything enough closer to understand my problem. I have gaps from zeros and islands from 15. You can see what I am talking about in ...
0
votes
2
answers
122
views
SQL Consecutive Date Cumulative Count
I am doing some roster analysis and need to identify when an employee has worked for 5 or more consecutive days. In my table, I can extract data something like the below (note, there are lot more ...