473,405 Members | 2,287 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Finding overlapping time intervals

5
Hello everybody!
I've been trying to find a way to create a MySQL query to find overlapping intervals, but no success so far. I've the following table as an example:

Src Dst Start_time End_time Bytes
A B 10 16 40
A B 8 13 20
A B 1 5 100
A C 9 15 40
A B 7 12 60

I would like to group by 'Src' and 'Dst' and have a result that looks like the following:

Src Dst Bytes
A B 120
A B 100
A C 40

Does anybody has any idea about how to create a query to achieve that? Any help will be appreciated!

Regards,
Oct 1 '07 #1
11 4945
Motoma
3,237 Expert 2GB
Could you explain what the logic is behind how you came to your result set? I am afraid it is not as apparent to us as it is to you.
Oct 2 '07 #2
corsin
5
Hi Motoma! Thanks for your reply!

The logic is as follows. I have several IP network flows in a MySQL database. These IP flows are basically represented as I wrote in my post (i.e., Src, Dst, Start_Time , End_Time, and amount of bytes). What I want to do is to merge those IP flows with the same source (Src) and Destination (Dst) that in some point in time overlapped.

Following the example that I gave to you we have...

Src Dst Start_time End_time Bytes
A B 10 16 40
A B 8 13 20
A B 1 5 100
A C 9 15 40
A B 7 12 60

... it's possible to see that there are 3 IP flows A B that overlapped in time: 1st row (with Start_time 10 and End_time 16), 2nd row (with Start_time 8 and End_time 13) and 5th row (with Start_time 7 and End_time 12). The IP flow A B in the 3rd row didn't overlap with any other IP flow A B so it does not have to be merged. It's interesting to notice that the IP flow A C (4th row) overlaps, but IP flow A C is different of IP flow A B (i.e., they have different destinations).

Therefore, the result that I would like to get is something like this:

Src Dst Start_time End_time Bytes
A B 7 16 120 *
A B 1 5 100
A C 9 15 40

* Notice here that the 'new' Start_time should be the earliest start_time (e.g., among the overlapped flows with Start_times 7, 8, and 10, 7 is the earliest) and the new End_time should be the latest end_time (among the overlapped flows with End_times 12, 13 and 16, 16 is the latest).

I hope I've made myself clearer now. If not, please let me know.

Regards,
Oct 2 '07 #3
Motoma
3,237 Expert 2GB
Hi Motoma! Thanks for your reply!

The logic is as follows. I have several IP network flows in a MySQL database. These IP flows are basically represented as I wrote in my post (i.e., Src, Dst, Start_Time , End_Time, and amount of bytes). What I want to do is to merge those IP flows with the same source (Src) and Destination (Dst) that in some point in time overlapped.

Following the example that I gave to you we have...

Src Dst Start_time End_time Bytes
A B 10 16 40
A B 8 13 20
A B 1 5 100
A C 9 15 40
A B 7 12 60

... it's possible to see that there are 3 IP flows A B that overlapped in time: 1st row (with Start_time 10 and End_time 16), 2nd row (with Start_time 8 and End_time 13) and 5th row (with Start_time 7 and End_time 12). The IP flow A B in the 3rd row didn't overlap with any other IP flow A B so it does not have to be merged. It's interesting to notice that the IP flow A C (4th row) overlaps, but IP flow A C is different of IP flow A B (i.e., they have different destinations).

Therefore, the result that I would like to get is something like this:

Src Dst Start_time End_time Bytes
A B 7 16 120 *
A B 1 5 100
A C 9 15 40

* Notice here that the 'new' Start_time should be the earliest start_time (e.g., among the overlapped flows with Start_times 7, 8, and 10, 7 is the earliest) and the new End_time should be the latest end_time (among the overlapped flows with End_times 12, 13 and 16, 16 is the latest).

I hope I've made myself clearer now. If not, please let me know.

Regards,
Sounds good, but describe how this data would be represented:
Src Dst Start_time End_time Bytes
A B 1 5 10
A B 4 9 10
A B 8 13 10
A C 1 5 10
A C 5 7 10
Oct 2 '07 #4
corsin
5
Sounds good, but describe how this data would be represented:
Src Dst Start_time End_time Bytes
A B 1 5 10
A B 4 9 10
A B 8 13 10
A C 1 5 10
A C 5 7 10
Hi!

Src, Dst = varchar(16)
Start_Time, End_Time, and Bytes = bigint(20)

If this is not the answer you're looking for, my apologizes since databases are not my field of expertise :)
Oct 2 '07 #5
Motoma
3,237 Expert 2GB
Hi!

Src, Dst = varchar(16)
Start_Time, End_Time, and Bytes = bigint(20)

If this is not the answer you're looking for, my apologizes since databases are not my field of expertise :)
Sorry, let me rephrase: Could you take a look at the dataset I gave, and tell me what the end result would be? That is to say, what do you want the SQL to return when the data is set up as I presented it earlier?
Oct 2 '07 #6
corsin
5
Sorry, let me rephrase: Could you take a look at the dataset I gave, and tell me what the end result would be? That is to say, what do you want the SQL to return when the data is set up as I presented it earlier?
Ok... I thought you meant the data type. Here we go then

Src Dst Start_time End_time Bytes
A B 1 5 10
A B 4 9 10
A B 8 13 10
A C 1 5 10
A C 5 7 10

The expected result should be:
Src Dst Start_time End_time Bytes
A B 1 13 30
A C 1 7 20

Regards,
Oct 2 '07 #7
Motoma
3,237 Expert 2GB
Ok... I thought you meant the data type. Here we go then

Src Dst Start_time End_time Bytes
A B 1 5 10
A B 4 9 10
A B 8 13 10
A C 1 5 10
A C 5 7 10

The expected result should be:
Src Dst Start_time End_time Bytes
A B 1 13 30
A C 1 7 20

Regards,

As I expected.
There is no good way to do this with SQL; you can do it with cursors, but my expectation is that it will be much slower than performing the same calculations with code.
Oct 2 '07 #8
corsin
5
As I expected.
There is no good way to do this with SQL; you can do it with cursors, but my expectation is that it will be much slower than performing the same calculations with code.
Hello Motoma!

Could you please show me an example of how cursors would do the job? I tried with code and it worked quite well with a small set of rows, but with a table of +or- 5000000 rows my program dies.

I've the SQL query below without recursivity, which results that I can't get all consecutive overlapping intervals:

with ver1 as (
select a.id AS origem,
b.id AS pertence
from VER a,
VER b
where
a.ipv4_src = b.ipv4_src and
a.ipv4_dst = b.ipv4_dst and
a.port_src = b.port_src and
a.port_dst = b.port_dst and
(a.start_time between b.start_time and b.end_time or
a.end_time between b.start_time and b.end_time)
),
v12 as (
select
distinct a.origem AS origem,
a.pertence AS pertence
from
ver1 a
union
select
distinct a.origem AS origem,
b.pertence AS pertence
from
ver1 a,
ver1 b
where
a.pertence = b.origem
),
v13 as (
select
distinct a.origem AS origem,
a.pertence AS pertence
from
v12 a
union
select
distinct a.origem AS origem,
b.pertence AS pertence
from
v12 a,
v12 b
where
a.pertence = b.origem
),
ver2 as (
select
distinct a.origem AS origem,
b.ipv4_src, b.ipv4_dst,
b.port_src, b.port_dst,
min(b.start_time) as start_time,
max(b.end_time) as end_time,
sum(b.nb_bytes) AS nb_bytes
from v13 a,
VER b
where
a.pertence = b.id and
not exists
(select 1
from ver1 c
where
c.pertence = a.origem and
c.origem a.origem)
group by
origem, b.ipv4_src, b.ipv4_dst, b.port_src, b.port_dst
)
select from ver2;
Oct 3 '07 #9
Motoma
3,237 Expert 2GB
Okay, here is the way I would do it, in descriptive pseudo code:
Expand|Select|Wrap|Line Numbers
  1. Create CURSOR list of unique paths (treat source-dest as PK)
  2. For each element in PathCursor
  3. {
  4.     Set totalBias = 0;
  5.     Set totalTransfered = 0;
  6.     Construct CURSOR which a UNION of two SELECTs, merge StartTime and EndTime into one column labeled Time, along with a colunm labeled Bias which is 1 for StartTimes, and -1 for EndTimes, and if it is an end time, include the BytesTransfered, ORDER the result set BY Time;
  7.     Set PathStartTime = 0;
  8.     For each element in TimeCursor
  9.     {
  10.         if PathStartTime = 0
  11.         {
  12.             PathStartTime = StartTime
  13.         }
  14.         Add Bias to TotalBias;
  15.         Add BytesTransfered to totalTransfered;
  16.         If TotalBias = 0
  17.         {
  18.             One row in your result set will be made of Path, PathStartTime, EndTime, totalBytes;
  19.             Set PathStartTime, totalByte = 0;
  20.         }
  21.     }
  22. }
  23.  
Oct 3 '07 #10
How about this:

SELECT b.id, 'Overlaps", a.id
from
(SELECT * from TBL) a,
(SELECT * from TBL) b,
where
a.id > b.id and
b.start >= a.start
and b.start <= a.end
order by b.id
Oct 18 '07 #11
Motoma
3,237 Expert 2GB
How about this:

SELECT b.id, 'Overlaps", a.id
from
(SELECT * from TBL) a,
(SELECT * from TBL) b,
where
a.id > b.id and
b.start >= a.start
and b.start <= a.end
order by b.id
Hi flgllm1! Welcome to The Scripts.
Looking at the original problem, I don't believe that a single JOIN will be able to perform the necessary operations: take for example this case.

Expand|Select|Wrap|Line Numbers
  1. start     end     tx
  2. 0         15      10
  3. 10        25      10
  4. 20        35      10
  5. 30        45      10
  6.  
This would be combined into one connection, with a start time of 0, an end time of 45, and a bytes transfered value of 40.
Oct 18 '07 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: Max M | last post by:
I am writing a "find-free-time" function for a calendar. There are a lot of time spans with start end times, some overlapping, some not. To find the free time spans, I first need to convert the...
3
by: Phil Sandler | last post by:
All, I have a table with start and end dates/times in it, and would like to be able to calculate the number of hours represented, accounting for overlapping records. Note that I am looking...
5
by: giampiero mu | last post by:
hi everyone my target is implement a function controlla(string - a binary string-) that check if there is in a string two equal not overlapping subsequences at least of length limitem: my...
4
by: Holger Marzen | last post by:
Say, we have uptimes from several servers: Server up_from up_to ------ ------- ------- s1 0:00 8:00 s1 10:00 20:00 s1 22:00 24:00 (would better be...
6
by: Penguin | last post by:
At some long ago time Steve Jorgensen answered thus: Subject: Re: How can I round a time? Newsgroups: comp.databases.ms-access Date: 1998/12/11 Access represents a date internally as a double...
2
by: rivka.howley | last post by:
I wrote some code that creates a table with a date/time field at 15-minute intervals. Here's how I create and populate the table With tblDataTemp ..Fields.Append .CreateField("CT_ID", dbLong)...
8
by: Rsapru | last post by:
i have a table containing following data effdate termdate uid ----------- ----------- ----------- 1 2 1 3 4 2 5 8 3 7 ...
2
by: Rombolt | last post by:
Hi I have a MSSQL table with many time intervals stored as datetime. Each time interval is also assigned a numeric type that specifies what type of job was done during the time interval. I need to...
7
by: Breal | last post by:
I have a list that looks like the following I would like to be able to determine which of these overlap each other. So, in this case, tuple 1 overlaps with tuples 2 and 3. Tuple 2 overlaps...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.