Rectangle 27 5

I haven't upgraded yet so I can't play around but based on this video and this article I can already see some advantages. They're not necessarily adding functionality that you couldn't do before, but rather just taking the hassle out of it. Using readtable over xlsread is immediately appealing to me. Being able to access columns by name rather than just by index is great, I do it in other languages often. In a table where column order doesn't really matter (unlike a matrix) it's really convenient to be able to address a column by it's name instead of having to know the column order. Also you can merge table using the join function which wasn't that easy to do with cell arrays before. I see that you can name the rows too, I didn't see what advantage that gives you and I can't play around but I know in some languages (like PANDAS in Python and I think in R as well) naming rows means you can work with time series data with different series that are not completely overlapping and not have to worry about alignment. I hope this is the case in Matlab too! Categorical arrays also look like just an extra layer of convenience, kind of like an enum. You never actually need a enum but it just makes development more pleasant.

Anyway that's just my two cents, I probably won't get an opportunity to play around with them any time soon but I look forward to using them when I do need them.

thanks for the answer and the links. I was surprised to learn that the question in on hold...

Likewise - the question is what is the difference, which isn't really a matter of opinion. I'd like to have seen what others know about them who've actually used them.

matlab - What are the differences between Tables and Categorical Array...

Rectangle 27 0

Most of the time, C2 will apply on top of C1. It's only if they're edits to overlapping sections of the same files that you'll get a merge conflict. Git will take all the parts of the patch that don't conflict, and insert conflict markers that help you resolve the merge.

As for what to do, it depends how large your project is and how competent you are in that section of the code -- I've always just fixed up the conflicts myself, as long as the submitter used a reasonably recent checkout as the base for their patch.

+1 from "another commenter";) My quote from Linux was a bit tongue in cheek, in that it isn't a universal solution, simply a good illustration of the DVCS process and its "publication" workflow (

open source - How are git patches handled in opensource projects? - St...

git open-source patch
Rectangle 27 0

The easiest way I can think of would be to slurp all records for the time range you're interested in, then "merge" records based on the well-known overlapping date formula:

List<Tuple<DateTime, DateTime>> dateRows = GetDateRowsSomehow();
//sorting by start time; you can do this in SQL pretty easily
//necessary to make sure the row most likely to overlap a given row is the next one
dateRows.Sort((a,b) => a.Item1.CompareTo(b.Item1));

for(var i=0; i<dateRows.Count - 1; i++)
    for(var j=i+1, j<dateRows.Count; j++)
        if(dateRows[i].Item1 <= dateRows[j].Item2
            && dateRows[i].Item2 >= dateRows[j].Item1) //overlap
           //keep date row i, with the values of the complete time range of i and j
           dateRows[i].Item1 = dateRows[i].Item1 < dateRows[j].Item1
               ? dateRows[i].Item1
               : dateRows[j].Item1;
           dateRows[i].Item2 = dateRows[i].Item2 > dateRows[j].Item2
               ? dateRows[i].Item2
               : dateRows[j].Item2;
           //remove row j and ensure we don't skip the row after it

This solution's WCS is a large result set with zero overlap, which will perform on the order of N(N-1)/2 = O(N2). The best case is linear (not counting the NlogN sort operation or the repeated linear shifting within the List), when all rows in question have some overlap with each other. You can't use foreach because we're changing the size of the collection as we move through it. There is probably a more efficient way (for instance traversing the list back-to-front, minimizing shifting), but this should be decent, and also importantly, clean and concise.

sql server - SQL or C#: Get all timespans without overlap - Stack Over...

c# sql-server math time
Rectangle 27 0

Here's a possible solution. The basic idea here is to compare lagged start date with the maximum end date "until now" using the cummax function and create an index that will separate the data into groups

data %>%
  group_by(ID) %>%
  mutate(indx = c(0, cumsum(as.numeric(lead(start)) >
                     cummax(as.numeric(end)))[-n()])) %>%
  group_by(ID, indx) %>%
  summarise(start = first(start), end = last(end))

# Source: local data frame [3 x 4]
# Groups: ID
#   ID indx      start        end
# 1  A    0 2013-01-01 2013-01-06
# 2  A    1 2013-01-07 2013-01-11
# 3  A    2 2013-01-12 2013-01-15

Thanks for such a great answer! Question though, when I used the function on the real data set the dates ended up kept in second format I had to wrap the summarise variables in as.POSIXct() to convert them back any ideas why?

Not sure what you mean by that... When I save the result in some variable , both start and end are of class POSIXct...

Btw if you use multiple ID's you have to arrange by arrange(data, ID, start) as lag is not affected by grouping and so may take the dates from outside the ID group messing up the final structure. This wasn't part of the question but I found out the hard way afterwords.

date - How to flatten/merge overlapping time periods in R - Stack Over...

r date lubridate
Rectangle 27 0

I only came up with a CTE query as the problem is there may be a chain of overlapping times, e.g. record 1 overlaps with record 2, record 2 with record 3 and so on. This is hard to resolve without CTE or some other kind of loops, etc. Please give it a go anyway.

The first part of the CTE query gets the services that start a new group and are do not have the same starting time as some other service (I need to have just one record that starts a group). The second part gets those that start a group but there's more then one with the same start time - again, I need just one of them. The last part recursively builds up on the starting group, taking all overlapping services.

Here is SQLFiddle with more records added to demonstrate different kinds of overlapping and duplicate times.

I couldn't use ServiceID as it would have to be ordered in the same way as BeginTime.

Thank you, Szymon, for coming up with this solution. The 'BeginTime' value won't be unique for each day but there is another field (ServiceID) that is unique for each record in the 'service' table. We can use that field to improve the query. I will test this solution and provide feedback soon. Again, thanks for your time!

Szymon, here is your query with sample data. The first two rows in the results set should be merged into one row (06:00-08:45) but it generates two rows (06:00-08:30 & 06:00-08:45)!6/bfbdc/3

sql server - Flatten/merge overlapping time intervals - Stack Overflow

sql-server sql-server-2008 group-by
Rectangle 27 0

I am not quite sure that I understand what data you actually need. From your explanation I think that you need all Jobs from Resource 1 and Jobs from Resource 2 that are not ovelapping Jobs from Resource 2. Here is my solution:

Actually in your example there is a mistake - you point out that the correct element is Job 5 from Resource 2, but it is covered by Job 1 Resource 1.

EDIT: This is the query you need (base on difference of the time by 1 minute)

Sorry, I miskeyed some of the dates. I put 2014-08-09 instead of 2014-08-01. Sorry

I tried your solution (with corrected data) and couldn't get it to work

I take your edited data and I receive as correct element only 6. 5 is not correct becase its start time 2014-08-01 10:00:00 is equal to end time of Job 1 (Resource 1). 7 and 8 time is inside time of Job 2 (Resource 1).

Take into account that for SQL start time 2014-08-01 10:00:00 is equal to end time 2014-08-01 10:00:00. If you think that one jobs ends at that time and next starts in exactly the same moment then you have to change end time in the comparison. For example substract 1 second and you will get result that you need in that case.

T-SQL Merge non overlapping times - Stack Overflow

sql merge overlap
Rectangle 27 0

I found its performance to be far better than the available ruby alternatives at the time for merging very large sets of overlapping CIDRs.

Ruby version of Perl Net::CIDR::Lite? - Stack Overflow

ruby perl
Rectangle 27 0

Merge Overlapping Intervals

Given a set of time intervals in any order, merge all overlapping intervals into one and output the result which should have only mutually exclusive intervals. Let the intervals be represented as pairs of integers for simplicity. For example, let the given set of intervals be {{1,3}, {2,4}, {5,7}, {6,8} }. The intervals {1,3} and {2,4} overlap with each other, so they should be merged and become {1, 4}. Similarly {5, 7} and {6, 8} should be merged and become {5, 8}

Write a function which produces the set of merged intervals for the given set of intervals.

A simple approach is to start from the first interval and compare it with all other intervals for overlapping, if it overlaps with any other interval, then remove the other interval from list and merge the other into the first interval. Repeat the same steps for remaining intervals after first. This approach cannot be implemented in better than O(n^2) time.

An efficient approach is to first sort the intervals according to starting time. Once we have the sorted intervals, we can combine all intervals in a linear traversal. The idea is, in sorted array of intervals, if interval[i] doesnt overlap with interval[i-1], then interval[i+1] cannot overlap with interval[i-1] because starting time of interval[i+1] must be greater than or equal to interval[i]. Following is the detailed step by step algorithm.

1. Sort the intervals based on increasing order of starting time.2. Push the first interval on to a stack.3. For each interval do the following ..a. If the current interval does not overlap with the stack top, push it. ..b. If the current interval overlaps with stack top and ending time of current interval is more than that of stack top, update stack top with the ending time of current interval.4. At the end stack contains the merged intervals.

Below is a C++ implementation of the above approach.

// A C++ program for merging overlapping intervals
#include <iostream>
#include <vector>
#include <algorithm>
#include <stack>
using namespace std;

// An interval has start time and end time
struct Interval
    int start;
    int end;

// Compares two intervals according to their staring time.
// This is needed for sorting the intervals using library
// function std::sort(). See
bool compareInterval(Interval i1, Interval i2)
{  return (i1.start < i2.start)? true: false; }

// The main function that takes a set of intervals, merges
// overlapping intervals and prints the result
void mergeIntervals(vector<Interval>& intervals)
    // Test if the given set has at least one interval
    if (intervals.size() <= 0)

    // Create an empty stack of intervals
    stack<Interval> s;

    // sort the intervals based on start time
    sort(intervals.begin(), intervals.end(), compareInterval);

    // push the first interval to stack

    // Start from the next interval and merge if necessary
    for (int i = 1 ; i < intervals.size(); i++)
        // get interval from stack top
        Interval top =;

        // if current interval is not overlapping with stack top,
        // push it to the stack
        if (top.end < intervals[i].start)
            s.push( intervals[i] );
        // Otherwise update the ending time of top if ending of current 
        // interval is more
        else if (top.end < intervals[i].end)
            top.end = intervals[i].end;

    // Print contents of stack
    cout << "\n The Merged Intervals are: ";
    while (!s.empty())
        Interval t =;
        cout << "[" << t.start << "," << t.end << "]" << " ";


// Functions to run test cases
void TestCase1()
    // Create a set of intervals
    Interval intvls[] = { {6,8}, {1,9}, {2,4}, {4,7} };
    vector<Interval> intervals(intvls, intvls+4);

    // Merge overlapping inervals and print result
void TestCase2()
    // Create a set of intervals
    Interval intvls[] = { {6,8},{1,3},{2,4},{4,7} };
    vector<Interval> intervals(intvls, intvls+4);

    // Merge overlapping inervals and print result
void TestCase3()
    // Create a set of intervals
    Interval intvls[] = { {1,3},{7,9},{4,6},{10,13} };
    vector<Interval> intervals(intvls, intvls+4);

    // Merge overlapping inervals and print result

// Driver program to test above functions
int main()
    return 0;


The Merged Intervals are: [1,9]
 The Merged Intervals are: [1,8]
 The Merged Intervals are: [10,13] [7,9] [4,6] [1,3]

Time complexity of the method is O(nLogn) which is for sorting. Once the array of intervals is sorted, merging takes linear time.

This article is compiled by Ravi Chandra Enaganti. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Writing code in comment?

Rectangle 27 0

You may also try this query (once more solutions beside those given by PM 77-1 in the comment above) :

Demo --->!12/bdf7e/9 however for huge table the performance of this query could be horribly slow, and some procedural approach might perform better.

Wow! Just now i saw your answer, I didn't receive any warning about this ;~ Thank you a lot man, your query works perfectly! I was a long time trying to do that.

How would you write this query for SQL Server 2008? SQL Server does not have the 'least' and 'greatest' functions.

sql - Merge overlapping time intervals, how? - Stack Overflow

sql postgresql merge overlap date-range
Rectangle 27 0

I think, in general, this problem could be solved by selecting all possible start dates into one dataset, and then for each start date get date from next row as end date of the period, something like this:

;with cte(StartDate) as (
    select distinct StartDate from Table1
    select distinct EndDate from Table1
    select distinct StartDate from Table2
    select distinct dateadd(dd, 1, EndDate) from Table2
), cte2 as (
        row_number() over(order by StartDate asc) as rn
    from cte
        when c2.rn = max(c2.rn) over() then c2.StartDate
        else dateadd(dd, -1, c2.StartDate)
    end as EndDate
from cte2 as c
    inner join cte2 as c2 on c2.rn = c.rn + 1

This code is for SQL Server, and the answer is also depends on if it's possible for end of period to be equal to start of next period or not, but you can get general idea.

@pamaxeed no prob, this code with CTE could be not very fast, you can use some temp table to store dates.

Hi Roman, thanks for the answer, it is very helpful for me. Just to understand ur approach: u build a temporary dataset (cte i think it is a transact sql keyword), where u store all StartDate and order it in ascending way. And then u make for each startdate, u retrieve the startdate of the next row and make it as enddate. Is it right? Why u add select distinct dateadd(dd, 1, EndDate) from Table2 ? And in the result the value is missing.

sql - Overlapping periods - Merging in a continous time series - Stack...

sql algorithm sorting oracle11g overlapping
Rectangle 27 0

Here is the script I wrote to merge a bunch of log files some time ago. I started using kdiff3 manually first which worked fine for small files but became painfully and eventually unusably slow as the accumulated log grew large...

Our logs contained regular occurrences of the result of printf("time(NULL) = %d\n", time(NULL));, you have to adapt to find some other monotonically increasing sync mark.

use strict;
use warnings;

# This program takes two overlapping log files and combines
# them into one, e.g.
#          INPUT:                    OUTPUT:
#   file1        file2              combined
#    AAA                               AAA
#    AAA                               AAA
#    AAA                               AAA
#    BBB          BBB                  BBB
#    BBB          BBB                  BBB
#    BBB          BBB                  BBB
#                 CCC                  CCC
#                 CCC                  CCC
#                 CCC                  CCC
#                 CCC                  CCC

# This programm uses the "time(NULL) = <...time...>" lines in the
# logs to match where the logs start overlapping.

# Example line matched with this function:
# time(NULL) = 1388772638
sub get_first_time_NULL {
    my $filename = shift;
    my $ret = undef;
    open(FILE, $filename);
    while (my $line = <FILE>) {
        if ($line =~ /^time\(NULL\) = (\d+)/) {
            $ret = $1;
    return $ret;

my $F1_first_time = get_first_time_NULL($ARGV[0]);
my $F2_first_time = get_first_time_NULL($ARGV[1]);

my $oldest_file;
my $newest_file;
my $newest_file_first_time;

if ($F1_first_time <= $F2_first_time) {
    $oldest_file = $ARGV[0];
    $newest_file = $ARGV[1];
    $newest_file_first_time = $F2_first_time;
} else {
    $oldest_file = $ARGV[1];
    $newest_file = $ARGV[0];
    $newest_file_first_time = $F1_first_time;

# Print the "AAA" part
open(FILE, $oldest_file);
while (my $line = <FILE>) {
    print $line;
    last if ($line =~ /^time\(NULL\) = $newest_file_first_time/);

# Print the "BBB" and "CCC" parts
my $do_print = 0;
open(FILE, $newest_file);
while (my $line = <FILE>) {
    print $line if $do_print;
    $do_print = 1 if ($line =~ /^time\(NULL\) = $newest_file_first_time/);

The above perl script only processes two files, so I wrote the following shell script to process all the log files in one operation:


# This script combines several overlapping logfiles into one
# continous one. See for more details into
# how the logs are merged, this script is only glue to process
# multiple files in one operation.

set -e


echo "Processing $1..."
cp "$1" MeRgE.TeMp.1

while [ -n "$1" ]
    if [ ! -s "$1" ]
        echo "Skipping empty file $1..."
    echo "Processing $1..."
    perl `echo $0 | sed 's/\.sh$/.pl/'` MeRgE.TeMp.1 "$1" > MeRgE.TeMp.2 && mv MeRgE.TeMp.2 MeRgE.TeMp.1

echo "Done"

merge - How to combine files like 'diff --unified' does it? - Stack Ov...

merge diff