Finding Overlapping Data Ranges

Kevin Feasel



Louis Davidson shows how to find groups of data which overlap:

This week, I had a problem where I needed to find and eliminate from the results of my query, data with overlapping ranges. I have written about this topic before, in my database design book book, in regards to building a trigger to avoid overlapping ranges. But even though I have written on the topic there, I still use Google just like you to get quick help (I use books when I want to learn, or expand my knowledge on a topic in depth, blogs when I need a simple answer to a simple or complex question.)

The problem at hand is most often associated with date based data, such as effective dates for a row in a temporal/type 2 dimension table, or other cases like appointment times, etc. But the algorithm is the same with numbers and is a bit easier to read since we don’t have the same issues with roundoff and decimal places (the query is complex enough on its own to show in a blog post). From a progression of start and end values in each row, we are going to look at how to check to make sure that there are no two rows that are in conflict (no range should contain another ranges value at all).

This feels like the type of thing which could be rewritten with window function to be a little smoother, but I’d have to think about it more.  Louis does provide a good solution and explanation to a fairly common but tricky problem in T-SQL.

Related Posts

Deferred Name Resolution In SQL Server

Kendra Little explains the concept of deferred name resolution in SQL Server: In this case, I’m creating a temporary stored procedure (out of laziness, it means I don’t have to clean up a quick demo) – CREATE OR ALTER PROCEDURE #test AS IF 1=0 EXECUTE dbdoesnotexist.dbo.someproc; GO The database dbdoesnotexist does NOT exist, but I’m […]

Read More

T-SQL Join Delete

Steve Stedman walks us through a bit of T-SQL proprietary syntax: 1 2 3 DELETE t2 FROM [dbo].[Table1] t1 INNER JOIN [dbo].[Table2] t2 on t1.favColor =; Names have been changed to protect the innocent. In the above delete statement which table will have rows deleted from it? A: Table1 B: Table2 C: Both Table1 […]

Read More


April 2018
« Mar May »