Find related rows that have “nothing in between them” Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) Data science time! April 2019 and salary with experience Should we burninate the [wrap] tag? The Ask Question Wizard is Live!Get a list of dates between two datesHow to retrieve row in DB Table just added now in JavaQuerying NULL values from table - SQLmysql hotel room availabilityFind a gap between time stampmysql relation slowSQL query to show the difference between multiple rowshow to trigger MySQL query when DATETIME is pastUpdating all rows in one table who have 1 match, more than 1 match, or 0 matches in other tableMySQL - Using COALESCE with DATE_ADD and DATE_SUB to get next/previous record
How to react to hostile behavior from a senior developer?
What exactly is a "Meth" in Altered Carbon?
Storing hydrofluoric acid before the invention of plastics
How come Sam didn't become Lord of Horn Hill?
String `!23` is replaced with `docker` in command line
Identify plant with long narrow paired leaves and reddish stems
How to align text above triangle figure
How to deal with a team lead who never gives me credit?
How does the particle を relate to the verb 行く in the structure「A を + B に行く」?
Why didn't this character "real die" when they blew their stack out in Altered Carbon?
When a candle burns, why does the top of wick glow if bottom of flame is hottest?
Bete Noir -- no dairy
Can a non-EU citizen traveling with me come with me through the EU passport line?
What does the "x" in "x86" represent?
What causes the vertical darker bands in my photo?
Coloring maths inside a tcolorbox
Book where humans were engineered with genes from animal species to survive hostile planets
In predicate logic, does existential quantification (∃) include universal quantification (∀), i.e. can 'some' imply 'all'?
At the end of Thor: Ragnarok why don't the Asgardians turn and head for the Bifrost as per their original plan?
What is a non-alternating simple group with big order, but relatively few conjugacy classes?
How discoverable are IPv6 addresses and AAAA names by potential attackers?
Why do people hide their license plates in the EU?
Why was the term "discrete" used in discrete logarithm?
Error "illegal generic type for instanceof" when using local classes
Find related rows that have “nothing in between them”
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)
Data science time! April 2019 and salary with experience
Should we burninate the [wrap] tag?
The Ask Question Wizard is Live!Get a list of dates between two datesHow to retrieve row in DB Table just added now in JavaQuerying NULL values from table - SQLmysql hotel room availabilityFind a gap between time stampmysql relation slowSQL query to show the difference between multiple rowshow to trigger MySQL query when DATETIME is pastUpdating all rows in one table who have 1 match, more than 1 match, or 0 matches in other tableMySQL - Using COALESCE with DATE_ADD and DATE_SUB to get next/previous record
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I have a table like so:
CREATE TABLE mytable (
id INT(10) auto_increment PRIMARY KEY,
from DATE(10) NOT NULL,
before DATE(10) NULL,
reference_id INT(10) NOT NULL,
)
So there are rows that reference another table (with reference_id). Those references have a date range (from/before) in which they are applicable. For each reference_id there may be many entries which usually have no gaps:
id | from | before | reference_id
-------------------------------------------
1 | 2019-03-01 | 2019-03-05 | 5
5 | 2019-03-05 | 2019-03-09 | 5
8 | 2019-03-09 | NULL | 5
(There might be entries for other reference_ids between them.) An entry begins where the previous ended. Now I want to find all entries that have a gap between them, where the from is later than the before of the preceding. For example (change in row 2, column from):
id | from | before | reference_id
-------------------------------------------
1 | 2019-03-01 | 2019-03-05 | 5
5 | 2019-03-06 | 2019-03-09 | 5
8 | 2019-03-09 | NULL | 5
Row 2's from is one day later than row 1's before, that's a gap. The problem: for rows 3 and 1 the same is true, but they should not be considered as result because they have another row between them.
What I came up with is this:
SELECT *
FROM mytable mt1
INNER JOIN mytable mt2 ON mt1.reference_id = mt2.reference_id AND mt1.id != mt2.id
WHERE mt1.before IS NOT NULL
AND mt1.from < mt2.from
AND DATE_ADD(mt1.before, INTERVAL 1 DAY) = mt2.from
AND NOT EXISTS(SELECT * FROM mytable mt3 WHERE mt3.id BETWEEN mt1.id AND mt2.id)
However, this (the EXISTS) is incredibly slow. Is there a better way to do this?
[edit]The query just finished and I didn't get any results although I'm definitly expecting some. So it's not only slow but also incorrect.[/edit]
Execution plan:
1,PRIMARY,mt1,ALL,"mytable_48d78c2b,mytable_261384ee,mytable_849034da",,,,3313021,Using where
1,PRIMARY,mt2,ref,"mytable_48d78c2b,mytable_849034da",mytable_849034da,4,db.mt1.reference_id,1,Using index condition; Using where
2,DEPENDENT SUBQUERY,mt3,index,PRIMARY,mytable_48d78c2b,3,,3313021,Using where; Using index
sql mariadb
|
show 1 more comment
I have a table like so:
CREATE TABLE mytable (
id INT(10) auto_increment PRIMARY KEY,
from DATE(10) NOT NULL,
before DATE(10) NULL,
reference_id INT(10) NOT NULL,
)
So there are rows that reference another table (with reference_id). Those references have a date range (from/before) in which they are applicable. For each reference_id there may be many entries which usually have no gaps:
id | from | before | reference_id
-------------------------------------------
1 | 2019-03-01 | 2019-03-05 | 5
5 | 2019-03-05 | 2019-03-09 | 5
8 | 2019-03-09 | NULL | 5
(There might be entries for other reference_ids between them.) An entry begins where the previous ended. Now I want to find all entries that have a gap between them, where the from is later than the before of the preceding. For example (change in row 2, column from):
id | from | before | reference_id
-------------------------------------------
1 | 2019-03-01 | 2019-03-05 | 5
5 | 2019-03-06 | 2019-03-09 | 5
8 | 2019-03-09 | NULL | 5
Row 2's from is one day later than row 1's before, that's a gap. The problem: for rows 3 and 1 the same is true, but they should not be considered as result because they have another row between them.
What I came up with is this:
SELECT *
FROM mytable mt1
INNER JOIN mytable mt2 ON mt1.reference_id = mt2.reference_id AND mt1.id != mt2.id
WHERE mt1.before IS NOT NULL
AND mt1.from < mt2.from
AND DATE_ADD(mt1.before, INTERVAL 1 DAY) = mt2.from
AND NOT EXISTS(SELECT * FROM mytable mt3 WHERE mt3.id BETWEEN mt1.id AND mt2.id)
However, this (the EXISTS) is incredibly slow. Is there a better way to do this?
[edit]The query just finished and I didn't get any results although I'm definitly expecting some. So it's not only slow but also incorrect.[/edit]
Execution plan:
1,PRIMARY,mt1,ALL,"mytable_48d78c2b,mytable_261384ee,mytable_849034da",,,,3313021,Using where
1,PRIMARY,mt2,ref,"mytable_48d78c2b,mytable_849034da",mytable_849034da,4,db.mt1.reference_id,1,Using index condition; Using where
2,DEPENDENT SUBQUERY,mt3,index,PRIMARY,mytable_48d78c2b,3,,3313021,Using where; Using index
sql mariadb
Any explain/execution plan (or similar)?
– jarlh
Mar 22 at 9:05
I'm using MariaDB
– Tim-Erwin
Mar 22 at 9:06
1
I added the execution plan
– Tim-Erwin
Mar 22 at 9:08
THB, I didn't think this would be an issue of setting an index here and there or doing engine specific stuff, rather me doing it plain wrong or naive or not how a SQL magician would do it.
– Tim-Erwin
Mar 22 at 9:10
Could there be overlap as well as gaps? for instancebeforeof row 2 which would be2019-03-10with unchanged row 3?
– Guillaume
Mar 22 at 10:53
|
show 1 more comment
I have a table like so:
CREATE TABLE mytable (
id INT(10) auto_increment PRIMARY KEY,
from DATE(10) NOT NULL,
before DATE(10) NULL,
reference_id INT(10) NOT NULL,
)
So there are rows that reference another table (with reference_id). Those references have a date range (from/before) in which they are applicable. For each reference_id there may be many entries which usually have no gaps:
id | from | before | reference_id
-------------------------------------------
1 | 2019-03-01 | 2019-03-05 | 5
5 | 2019-03-05 | 2019-03-09 | 5
8 | 2019-03-09 | NULL | 5
(There might be entries for other reference_ids between them.) An entry begins where the previous ended. Now I want to find all entries that have a gap between them, where the from is later than the before of the preceding. For example (change in row 2, column from):
id | from | before | reference_id
-------------------------------------------
1 | 2019-03-01 | 2019-03-05 | 5
5 | 2019-03-06 | 2019-03-09 | 5
8 | 2019-03-09 | NULL | 5
Row 2's from is one day later than row 1's before, that's a gap. The problem: for rows 3 and 1 the same is true, but they should not be considered as result because they have another row between them.
What I came up with is this:
SELECT *
FROM mytable mt1
INNER JOIN mytable mt2 ON mt1.reference_id = mt2.reference_id AND mt1.id != mt2.id
WHERE mt1.before IS NOT NULL
AND mt1.from < mt2.from
AND DATE_ADD(mt1.before, INTERVAL 1 DAY) = mt2.from
AND NOT EXISTS(SELECT * FROM mytable mt3 WHERE mt3.id BETWEEN mt1.id AND mt2.id)
However, this (the EXISTS) is incredibly slow. Is there a better way to do this?
[edit]The query just finished and I didn't get any results although I'm definitly expecting some. So it's not only slow but also incorrect.[/edit]
Execution plan:
1,PRIMARY,mt1,ALL,"mytable_48d78c2b,mytable_261384ee,mytable_849034da",,,,3313021,Using where
1,PRIMARY,mt2,ref,"mytable_48d78c2b,mytable_849034da",mytable_849034da,4,db.mt1.reference_id,1,Using index condition; Using where
2,DEPENDENT SUBQUERY,mt3,index,PRIMARY,mytable_48d78c2b,3,,3313021,Using where; Using index
sql mariadb
I have a table like so:
CREATE TABLE mytable (
id INT(10) auto_increment PRIMARY KEY,
from DATE(10) NOT NULL,
before DATE(10) NULL,
reference_id INT(10) NOT NULL,
)
So there are rows that reference another table (with reference_id). Those references have a date range (from/before) in which they are applicable. For each reference_id there may be many entries which usually have no gaps:
id | from | before | reference_id
-------------------------------------------
1 | 2019-03-01 | 2019-03-05 | 5
5 | 2019-03-05 | 2019-03-09 | 5
8 | 2019-03-09 | NULL | 5
(There might be entries for other reference_ids between them.) An entry begins where the previous ended. Now I want to find all entries that have a gap between them, where the from is later than the before of the preceding. For example (change in row 2, column from):
id | from | before | reference_id
-------------------------------------------
1 | 2019-03-01 | 2019-03-05 | 5
5 | 2019-03-06 | 2019-03-09 | 5
8 | 2019-03-09 | NULL | 5
Row 2's from is one day later than row 1's before, that's a gap. The problem: for rows 3 and 1 the same is true, but they should not be considered as result because they have another row between them.
What I came up with is this:
SELECT *
FROM mytable mt1
INNER JOIN mytable mt2 ON mt1.reference_id = mt2.reference_id AND mt1.id != mt2.id
WHERE mt1.before IS NOT NULL
AND mt1.from < mt2.from
AND DATE_ADD(mt1.before, INTERVAL 1 DAY) = mt2.from
AND NOT EXISTS(SELECT * FROM mytable mt3 WHERE mt3.id BETWEEN mt1.id AND mt2.id)
However, this (the EXISTS) is incredibly slow. Is there a better way to do this?
[edit]The query just finished and I didn't get any results although I'm definitly expecting some. So it's not only slow but also incorrect.[/edit]
Execution plan:
1,PRIMARY,mt1,ALL,"mytable_48d78c2b,mytable_261384ee,mytable_849034da",,,,3313021,Using where
1,PRIMARY,mt2,ref,"mytable_48d78c2b,mytable_849034da",mytable_849034da,4,db.mt1.reference_id,1,Using index condition; Using where
2,DEPENDENT SUBQUERY,mt3,index,PRIMARY,mytable_48d78c2b,3,,3313021,Using where; Using index
sql mariadb
sql mariadb
edited Mar 22 at 12:38
Tim-Erwin
asked Mar 22 at 9:02
Tim-ErwinTim-Erwin
5691422
5691422
Any explain/execution plan (or similar)?
– jarlh
Mar 22 at 9:05
I'm using MariaDB
– Tim-Erwin
Mar 22 at 9:06
1
I added the execution plan
– Tim-Erwin
Mar 22 at 9:08
THB, I didn't think this would be an issue of setting an index here and there or doing engine specific stuff, rather me doing it plain wrong or naive or not how a SQL magician would do it.
– Tim-Erwin
Mar 22 at 9:10
Could there be overlap as well as gaps? for instancebeforeof row 2 which would be2019-03-10with unchanged row 3?
– Guillaume
Mar 22 at 10:53
|
show 1 more comment
Any explain/execution plan (or similar)?
– jarlh
Mar 22 at 9:05
I'm using MariaDB
– Tim-Erwin
Mar 22 at 9:06
1
I added the execution plan
– Tim-Erwin
Mar 22 at 9:08
THB, I didn't think this would be an issue of setting an index here and there or doing engine specific stuff, rather me doing it plain wrong or naive or not how a SQL magician would do it.
– Tim-Erwin
Mar 22 at 9:10
Could there be overlap as well as gaps? for instancebeforeof row 2 which would be2019-03-10with unchanged row 3?
– Guillaume
Mar 22 at 10:53
Any explain/execution plan (or similar)?
– jarlh
Mar 22 at 9:05
Any explain/execution plan (or similar)?
– jarlh
Mar 22 at 9:05
I'm using MariaDB
– Tim-Erwin
Mar 22 at 9:06
I'm using MariaDB
– Tim-Erwin
Mar 22 at 9:06
1
1
I added the execution plan
– Tim-Erwin
Mar 22 at 9:08
I added the execution plan
– Tim-Erwin
Mar 22 at 9:08
THB, I didn't think this would be an issue of setting an index here and there or doing engine specific stuff, rather me doing it plain wrong or naive or not how a SQL magician would do it.
– Tim-Erwin
Mar 22 at 9:10
THB, I didn't think this would be an issue of setting an index here and there or doing engine specific stuff, rather me doing it plain wrong or naive or not how a SQL magician would do it.
– Tim-Erwin
Mar 22 at 9:10
Could there be overlap as well as gaps? for instance
before of row 2 which would be 2019-03-10 with unchanged row 3?– Guillaume
Mar 22 at 10:53
Could there be overlap as well as gaps? for instance
before of row 2 which would be 2019-03-10 with unchanged row 3?– Guillaume
Mar 22 at 10:53
|
show 1 more comment
1 Answer
1
active
oldest
votes
(Note that from is a very bad choice for a column name as it is a reserved keyword. But well, it is valid, so there we go.)
Assuming no overlap, you can use the lag function to look up the before of the previous row: lag(before, 1) over (partition by reference_id order by before) as previous_before. From there on, if it easy to check if there is a gap, if from > previous_before
select
`from`, before, `reference_id`
, `from` > lag(before, 1) over (partition by reference_id order by before) as has_gap
from mytable
What the lag does is to find the before value of the previous row, previous being determined by the order by clause of the window function. If from and before are identical, there is no gap.
This query gives you row with a gap before them, you can use the lead function in the same way to get rows with a gap after them.
Note that MariaDB has window functions since 10.2.2 only.
Thanks so much already. However, a "previous" row should be one with the samereference_id. Maybe I could solve that withorder by reference_idfirst. Apart from that I don't have MariaDB 10.2. Even the most recent Ubuntu is only on 10.1. If there's no other solution I'll try do get that version somehow.
– Tim-Erwin
Mar 22 at 12:36
@Tim-Erwin I forgot about the samereference_id, I fixed the query (by adding thepartition byclause). If you have enough control on your server, you can just add the relevant MariaDB repo and use a newer version: downloads.mariadb.org/mariadb/repositories/… The window functions are well worth the update (and there are new things with virtual columns as well which are very interesting).
– Guillaume
Mar 22 at 13:19
thanks for the query. I was able to update to 10.3. I had to wrap the query in another SELECT so I only get those that have a gap, works like a charm!
– Tim-Erwin
Mar 26 at 13:41
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55296104%2ffind-related-rows-that-have-nothing-in-between-them%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
(Note that from is a very bad choice for a column name as it is a reserved keyword. But well, it is valid, so there we go.)
Assuming no overlap, you can use the lag function to look up the before of the previous row: lag(before, 1) over (partition by reference_id order by before) as previous_before. From there on, if it easy to check if there is a gap, if from > previous_before
select
`from`, before, `reference_id`
, `from` > lag(before, 1) over (partition by reference_id order by before) as has_gap
from mytable
What the lag does is to find the before value of the previous row, previous being determined by the order by clause of the window function. If from and before are identical, there is no gap.
This query gives you row with a gap before them, you can use the lead function in the same way to get rows with a gap after them.
Note that MariaDB has window functions since 10.2.2 only.
Thanks so much already. However, a "previous" row should be one with the samereference_id. Maybe I could solve that withorder by reference_idfirst. Apart from that I don't have MariaDB 10.2. Even the most recent Ubuntu is only on 10.1. If there's no other solution I'll try do get that version somehow.
– Tim-Erwin
Mar 22 at 12:36
@Tim-Erwin I forgot about the samereference_id, I fixed the query (by adding thepartition byclause). If you have enough control on your server, you can just add the relevant MariaDB repo and use a newer version: downloads.mariadb.org/mariadb/repositories/… The window functions are well worth the update (and there are new things with virtual columns as well which are very interesting).
– Guillaume
Mar 22 at 13:19
thanks for the query. I was able to update to 10.3. I had to wrap the query in another SELECT so I only get those that have a gap, works like a charm!
– Tim-Erwin
Mar 26 at 13:41
add a comment |
(Note that from is a very bad choice for a column name as it is a reserved keyword. But well, it is valid, so there we go.)
Assuming no overlap, you can use the lag function to look up the before of the previous row: lag(before, 1) over (partition by reference_id order by before) as previous_before. From there on, if it easy to check if there is a gap, if from > previous_before
select
`from`, before, `reference_id`
, `from` > lag(before, 1) over (partition by reference_id order by before) as has_gap
from mytable
What the lag does is to find the before value of the previous row, previous being determined by the order by clause of the window function. If from and before are identical, there is no gap.
This query gives you row with a gap before them, you can use the lead function in the same way to get rows with a gap after them.
Note that MariaDB has window functions since 10.2.2 only.
Thanks so much already. However, a "previous" row should be one with the samereference_id. Maybe I could solve that withorder by reference_idfirst. Apart from that I don't have MariaDB 10.2. Even the most recent Ubuntu is only on 10.1. If there's no other solution I'll try do get that version somehow.
– Tim-Erwin
Mar 22 at 12:36
@Tim-Erwin I forgot about the samereference_id, I fixed the query (by adding thepartition byclause). If you have enough control on your server, you can just add the relevant MariaDB repo and use a newer version: downloads.mariadb.org/mariadb/repositories/… The window functions are well worth the update (and there are new things with virtual columns as well which are very interesting).
– Guillaume
Mar 22 at 13:19
thanks for the query. I was able to update to 10.3. I had to wrap the query in another SELECT so I only get those that have a gap, works like a charm!
– Tim-Erwin
Mar 26 at 13:41
add a comment |
(Note that from is a very bad choice for a column name as it is a reserved keyword. But well, it is valid, so there we go.)
Assuming no overlap, you can use the lag function to look up the before of the previous row: lag(before, 1) over (partition by reference_id order by before) as previous_before. From there on, if it easy to check if there is a gap, if from > previous_before
select
`from`, before, `reference_id`
, `from` > lag(before, 1) over (partition by reference_id order by before) as has_gap
from mytable
What the lag does is to find the before value of the previous row, previous being determined by the order by clause of the window function. If from and before are identical, there is no gap.
This query gives you row with a gap before them, you can use the lead function in the same way to get rows with a gap after them.
Note that MariaDB has window functions since 10.2.2 only.
(Note that from is a very bad choice for a column name as it is a reserved keyword. But well, it is valid, so there we go.)
Assuming no overlap, you can use the lag function to look up the before of the previous row: lag(before, 1) over (partition by reference_id order by before) as previous_before. From there on, if it easy to check if there is a gap, if from > previous_before
select
`from`, before, `reference_id`
, `from` > lag(before, 1) over (partition by reference_id order by before) as has_gap
from mytable
What the lag does is to find the before value of the previous row, previous being determined by the order by clause of the window function. If from and before are identical, there is no gap.
This query gives you row with a gap before them, you can use the lead function in the same way to get rows with a gap after them.
Note that MariaDB has window functions since 10.2.2 only.
edited Mar 22 at 21:08
answered Mar 22 at 11:02
GuillaumeGuillaume
1,54011227
1,54011227
Thanks so much already. However, a "previous" row should be one with the samereference_id. Maybe I could solve that withorder by reference_idfirst. Apart from that I don't have MariaDB 10.2. Even the most recent Ubuntu is only on 10.1. If there's no other solution I'll try do get that version somehow.
– Tim-Erwin
Mar 22 at 12:36
@Tim-Erwin I forgot about the samereference_id, I fixed the query (by adding thepartition byclause). If you have enough control on your server, you can just add the relevant MariaDB repo and use a newer version: downloads.mariadb.org/mariadb/repositories/… The window functions are well worth the update (and there are new things with virtual columns as well which are very interesting).
– Guillaume
Mar 22 at 13:19
thanks for the query. I was able to update to 10.3. I had to wrap the query in another SELECT so I only get those that have a gap, works like a charm!
– Tim-Erwin
Mar 26 at 13:41
add a comment |
Thanks so much already. However, a "previous" row should be one with the samereference_id. Maybe I could solve that withorder by reference_idfirst. Apart from that I don't have MariaDB 10.2. Even the most recent Ubuntu is only on 10.1. If there's no other solution I'll try do get that version somehow.
– Tim-Erwin
Mar 22 at 12:36
@Tim-Erwin I forgot about the samereference_id, I fixed the query (by adding thepartition byclause). If you have enough control on your server, you can just add the relevant MariaDB repo and use a newer version: downloads.mariadb.org/mariadb/repositories/… The window functions are well worth the update (and there are new things with virtual columns as well which are very interesting).
– Guillaume
Mar 22 at 13:19
thanks for the query. I was able to update to 10.3. I had to wrap the query in another SELECT so I only get those that have a gap, works like a charm!
– Tim-Erwin
Mar 26 at 13:41
Thanks so much already. However, a "previous" row should be one with the same
reference_id. Maybe I could solve that with order by reference_id first. Apart from that I don't have MariaDB 10.2. Even the most recent Ubuntu is only on 10.1. If there's no other solution I'll try do get that version somehow.– Tim-Erwin
Mar 22 at 12:36
Thanks so much already. However, a "previous" row should be one with the same
reference_id. Maybe I could solve that with order by reference_id first. Apart from that I don't have MariaDB 10.2. Even the most recent Ubuntu is only on 10.1. If there's no other solution I'll try do get that version somehow.– Tim-Erwin
Mar 22 at 12:36
@Tim-Erwin I forgot about the same
reference_id, I fixed the query (by adding the partition by clause). If you have enough control on your server, you can just add the relevant MariaDB repo and use a newer version: downloads.mariadb.org/mariadb/repositories/… The window functions are well worth the update (and there are new things with virtual columns as well which are very interesting).– Guillaume
Mar 22 at 13:19
@Tim-Erwin I forgot about the same
reference_id, I fixed the query (by adding the partition by clause). If you have enough control on your server, you can just add the relevant MariaDB repo and use a newer version: downloads.mariadb.org/mariadb/repositories/… The window functions are well worth the update (and there are new things with virtual columns as well which are very interesting).– Guillaume
Mar 22 at 13:19
thanks for the query. I was able to update to 10.3. I had to wrap the query in another SELECT so I only get those that have a gap, works like a charm!
– Tim-Erwin
Mar 26 at 13:41
thanks for the query. I was able to update to 10.3. I had to wrap the query in another SELECT so I only get those that have a gap, works like a charm!
– Tim-Erwin
Mar 26 at 13:41
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55296104%2ffind-related-rows-that-have-nothing-in-between-them%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Any explain/execution plan (or similar)?
– jarlh
Mar 22 at 9:05
I'm using MariaDB
– Tim-Erwin
Mar 22 at 9:06
1
I added the execution plan
– Tim-Erwin
Mar 22 at 9:08
THB, I didn't think this would be an issue of setting an index here and there or doing engine specific stuff, rather me doing it plain wrong or naive or not how a SQL magician would do it.
– Tim-Erwin
Mar 22 at 9:10
Could there be overlap as well as gaps? for instance
beforeof row 2 which would be2019-03-10with unchanged row 3?– Guillaume
Mar 22 at 10:53