1,456
questions
0
votes
2
answers
46
views
Find a gap > 10min and select begin/end dates in timestamp logs
Lets say I have a table with logs. Two columns: one with user name and second with record timestamp.
Records appear in table every 10 minutes for each user IF this user was active.
user
timestamp
...
1
vote
2
answers
30
views
How to create an SCD2 type table from partitioned table PostgreSQL/Clickhouse?
I have a table partitioned by date-type column ds, with many fields.
Not all columns change every day, therefore most rows are just duplicates of the previous rows.
I want to create an SCD2 table from ...
3
votes
2
answers
93
views
Insert missing rows in time series consumption history
I'm looking for assistance with writing a PostgreSQL query in Apache Superset.
I have metered consumption data, where the meters have unique IDs. Normally, a meter is read at the start of every hour, ...
0
votes
1
answer
52
views
Identifying Overlapping Appointments with One Non-Overbooked Occurrence
Scenario:
Appointments are made with start and end times. Appointments are allowed to overlap, but for reporting purposes, we need to know when there are overlapping appointments (called overbooking) ...
-1
votes
1
answer
57
views
Get ID ranges for multiple sections
How do I get the lower- and upper-bound range(s) of the following data for each set of "1s":
LogFileTextID
ErrorLineFlag
88182351
0
88182352
0
88182353
0
88182354
0
88182355
1
...
4
votes
3
answers
144
views
Perl: makes array of item IDs shortened
I have a large set of numbers, basically 1001 .. 150000 for a database using MySQL
There are a ton of gaps in the IDs on the database, so not all IDs exist. It can go from 100000 - 10500, then the ...
0
votes
2
answers
69
views
The problem of extracting consecutive dates in SQL query
There is a table of customers with purchases made in a certain month. It happens that a customer did not buy anything in a certain month, so this month is skipped.
Task:
I need to extract only those ...
3
votes
3
answers
104
views
Finding Product Switches on Account Between Dates
I am working on a problem were we have a table that holds id, product code , accountid , date
CREATE TABLE AccountBalanceAndProduct
(
[ID] [int] NOT NULL IDENTITY(1, 1),
[EffectiveDate] [date] ...
1
vote
1
answer
53
views
SQL Grouping based on one field and a flag
I struggle to get an sql that groups my records based on the following input data
AB
BIS
MyHash
flag
2024-01-01
2024-01-15
111
0
2024-01-16
2024-03-01
111
0
2024-03-02
2024-03-31
222
0
2024-04-01
...
0
votes
0
answers
40
views
Calculate Total Continuous Date Range
I'm pretty new to SAS and I have a dataset with a lot of overlapping date ranges for different ID numbers.
Essentially I'm trying to find out if any particular ID had a total continuous date range of ...
2
votes
2
answers
91
views
How do I a gap and island analysis with a power query?
I have a dataset similar to the one below, and I am trying to do a gap and island analysis on it.
Item
TransDate
InventoryCount
explanation
Bicycle
01/01/2023
0
First time the item Bicycle has been ...
2
votes
3
answers
88
views
How to performantly self-join same table multiple times with ascending and descending sort order?
Data Definition
I have a status table like so, attributes not relevant to the question are omitted:
id
created
value
1
2024-06-24T13:01:00
error
2
2024-06-24T13:02:00
ok
3
2024-06-24T13:03:00
...
0
votes
1
answer
67
views
How can I find the longest number of consecutive nulls in a column in MySQL?
Supposing I have a table consisting of something like:
date | A | B | C |
02/01/2020 | 3 | 5 | null |
03/01/2020 | 8 | 3 | null |
04/01/2020 | 3 | 4 | null |
05/01/2020 | 5 | 9 | 3 |
06/01/...
0
votes
1
answer
74
views
Calculate streaks at scale
So I need to calculate the streaks of a user (the number of consecutive days he created an entry). For the moment, I made a query that groups entries and it gives me the last group of streaks. Here is ...
0
votes
1
answer
42
views
Overall Order from a Reduced Dataset Against a Large Dataset
There is a scheme used in a process to create a delta of changes in a dataset from one day to the next. The query needs to get the overall order of a record within the entire result set of a view, ...
1
vote
3
answers
111
views
Compute column value based on previous row recursively
Given the following scheme and values I want to compute the value of each row starting by adding 1 to the previous row value. If the row value is null then set null. If there is a value then add 1 ...
0
votes
0
answers
26
views
Create a field that counts and groups "chains" of dates within +/- 3 days of each other in SQL. (Translate from PEEK function in QlikSense)
New to SQL and trying to translate a QlikSense calculation into Snowflake SQL
I have a dataset ordered by date/timestamp where I am trying to group dates that fall within 3 days of each other into &...
1
vote
1
answer
65
views
Postgres search missing date in hourly sequence
I have a table period_of_hours in PostgreSQL. This table contains several column but for my case is important column timestamp (timestamp without time zone) and key(character varying).
I have a ...
0
votes
3
answers
77
views
How to find gaps in an int column (grouped by another column) in MySQL?
There are numerous SQL questions and answers about finding gaps in sequences, but I could not find any that have the wrinkle my situation has: I need to group the sequence by another column.
My table (...
1
vote
1
answer
82
views
Populate balance for missing days
I have a table holding the daily balances of each account, but only on days on which there has been a transaction on the account. For the following account for example :
account_id | ...
1
vote
1
answer
35
views
SQL - grouping instances together
In SQL I am trying to group these values into single events, so that table 1 results in table 2.
Table 1
id
Value
1
0
2
1
3
2
4
3
5
0
6
1
7
2
8
3
9
4
10
5
11
0
12
1
13
2
14
3
15
4
16
...
1
vote
3
answers
114
views
DAX - Count max consecutives days with events
I'd like to count the consecutives days in month / year.
My data looks like :
google sheet link
In this situation, I need to find :
Max consecutives days in may : 3
in june : 6
max total : 6
option :...
0
votes
1
answer
33
views
SQL Problem : Use min and max in chronological groups
I would like to know if it is possible to group ONLY in chronological order and display the min and max according to the groups.
An example :
TYPE
Start_dath
End_dath
TL1
01/01
31/01
TL1
01/02
29/...
0
votes
3
answers
88
views
How to construct distinct date ranges from a set of ranges in sql
I have a table with a boolean field I care about, along with the date ranges those fields are relevant. These date ranges can be overlapping, can be entirely interior to other date ranges, and can ...
2
votes
1
answer
57
views
PostgreSQL cluster uninterrupted series of data
here's an example of the input and the desired output, I think this is easier to grasp than me describing the need.
input data
(business logic explanation: everything with a duration > 1M is set ...
0
votes
3
answers
80
views
Count consecutive number of True flags for each day before False occurs
Could you please help me with the following SQL task. Here's a data table:
| date | hour | flag |
|---------------|----------|----------|
| 2024-04-13 | 23 | True |
| ...
4
votes
3
answers
96
views
Postgres Select Row From Group With Opposite Sort Order
I have a Postgres table foobar with the following schema:
id: integer
group: integer
foo: integer
bar: integer
timestamp: integer
I keep track of updates to various groups, and those updates mutate ...
0
votes
1
answer
66
views
Query Students with 3 Consecutive Absences - Custom Calendar
Working on identifying students that have 3 or more consecutive absences on a custom calendar (already created) for the school year.
I have a temp table for the custom calendar here:
SELECT
cal....
1
vote
1
answer
47
views
Use interpolated values to fill missing rows in a column
I have per ID monthly cumulative counters, where in some months the counters are missing. I would like to replace these null entries with values interpolated between the nearest preceding and the ...
1
vote
1
answer
98
views
How to replace null values with the last preceding not-null value? [duplicate]
I have a table orders in Postgres with columns offer_id and date. I need to write a query which fills in all null cases in column offer_id, using the previous not-null value.
I tried to use the window ...
1
vote
1
answer
65
views
Consolidating Repeating Rows & Removing Duplicates
I am using SQL to store Log Data. Inside the Log Data there are repeating or duplicate consecutive rows that I would like to condense into a single row. The new row needs to reflect the start and end ...
0
votes
0
answers
61
views
SQL explode records that have overlapping date ranges
I have a SQL table containing rental contracts for cars, each with a start date and an end date. Some contracts referencing the same car have overlapping date ranges, and I'm seeking advice on how to ...
0
votes
1
answer
145
views
LAG() and LAST_VALUE in snowflake
I have a table that has data looks like:
date
qlfd_lvl
actv_ind
last_value
20230301
4
1
4
20230302
2
1
2
20230303
3
0
2
20230304
1
0
2
20230305
3
1
3
20230306
2
0
3
20230307
1
0
3
we have 2 ...
0
votes
3
answers
34
views
Converting timestamped values into value in a range
So I have a data like:
TimeStamp
Value
ts1
10
ts2
20
ts3
20
ts4
30
ts5
40
ts6
40
ts7
10
I want to change it to depict for how long a value sustained
For eg, for the above table the output ...
0
votes
0
answers
64
views
SQL: Assigning occasion numbers to GPS points when visiting places based on conditions
I have a PostgreSQL table of GPS points from multiple people with the information on the trip and place each of the points is assigned to. I want to find the occasion number at which each point_id is ...
0
votes
3
answers
284
views
MySQL query to find the customers who have placed orders on consecutive days
I am trying to get the ids of the customers who have placed orders on consecutive days. The table is created as follows:
create table orders(
orderid INT,
orderdate date,
customerid int
);
The ...
3
votes
3
answers
96
views
Query to sum over multiple rows based on column
I have a table with monthly amounts per ID, where in some of these months an invoice takes place.
My table looks like:
ID
Date
Invoiced
Amount
AAA
2023-01
0
10
AAA
2023-02
0
15
AAA
2023-03
1
15
...
0
votes
0
answers
52
views
SQL 2019 -Group by Id and Date field
SQL 2019 I'm having difficulty to generate similar groups by date.
This is the source table and image what is expected
IF (SELECT OBJECT_ID('tempdb..#EmpType')) IS NOT NULL
DROP TABLE #EmpType
...
0
votes
0
answers
19
views
Sessionize by source and duration in PostgreSQL
For a sample like
create table events (
uid int,
ts timestamp
);
insert into events values
-- uid session_id
-- 1 1
(1, '2024-02-01 00:10:00'),
(1, '2024-02-02 00:10:00'),
(1, '2024-02-02 00:20:...
-1
votes
1
answer
55
views
Rollup values between numbers > zero
I have the following table of which I want to get a count between the numbers > 0. Given the table I would like to see the results of value > 0 in columns and the count of 0.0 records until the ...
1
vote
2
answers
70
views
Values based on preceding rows: when x>2 then repeat 1, when x=0 for 3 consecutive rows then repeat 0
My task:
The variables x and def are a counter and a boolean respectively. Both start at zero.
If at any point in time x > 2 then def = 1 from that point onward.
Now if def = 1 then in order for ...
1
vote
2
answers
81
views
How to get previous record by date in case of no match in a join condition
I have two tables:
dates:
date
2024-01-01
2024-01-02
2024-01-03
2024-01-04
2024-01-05
actual:
business_date
id
2024-01-01
1
2024-01-02
2
2024-01-04
4
2024-01-05
5
I want to join these ...
1
vote
2
answers
55
views
PostgresSql backfill data if doesn't exist for a start period
Let's assume I have a table with 3 columns: Id, Value, date Time. I need to find the min, max, first and last value of each 1 minute window between the provided start and end time. And in case no row ...
0
votes
2
answers
52
views
How to turn an operation and timestamp in rows into separate columns using PL SQL
I tried using sql pivot, but it's not working, getting null value.
Before data:
NAME OPERATION TIMESTAMP
A START 18/01/2024 8:24
A START 18/01/2024 8:27
A START ...
0
votes
3
answers
58
views
How to group data where no logical grouping is available in oracle
I have a data like below in table
and I want data like below
If there is any gap between the dates then it should check the next day whether the data available or not if available the end date ...
0
votes
2
answers
158
views
Group by columns when date ranges are contiguous
I have data in a format like this:
DECLARE @WidgetPrice TABLE (WidgetPriceId BIGINT IDENTITY(1,1), WidgitId INT, Price MONEY,
StartEffectiveWhen DATE, EndEffectiveWhen DATE)
INSERT INTO @...
0
votes
2
answers
104
views
Get records that have exactly (n) records in a row continuously
Please consider this records:
Name
Year
Season
Name1
2011
1
Name1
2011
2
Name2
2012
1
Name2
2012
2
Name2
2012
3
Name2
2012
4
Name3
2012
3
Name3
2012
4
Name4
2012
1
Name4
2012
4
Name5
2012
...
2
votes
2
answers
81
views
Cross timeline count across subscriptions for customers
I have a subscription table with 4 fields: id, customer_id, start_date and end_date.
It lists all subscriptions of my customers. No subscription has an empty end_date, and a customer can have several ...
0
votes
1
answer
43
views
Oracle rank a chronological range of records based on a condition
Let's say I have a table containing some GPS points with their speed attribute
(let's ignore position here):
ID TIME SPEED
---------------------------------
1 2024-01-01 09:00:...
1
vote
2
answers
66
views
How to create sequence data with SQL Server?
I have the data like this.
ID
prev_org
org
event
event_date
1
-
A
Hire
202301
1
A
B
Transfer
202304
2
-
A
Hire
202301
2
-
A
Leave
202304
2
-
A
Return
202311
2
A
B
Transfer
202401
And What I ...