Getting Started with Adobe After Effects - Part 6: Motion Blur
First Time? You can support us by signing up. It takes only 5 seconds. Click here to sign up. If you already have an account, click here to login.

TSQL Challenge 80 - Find the minimal set of intervals without creating gaps

The challenge is to find the minimal set of intervals without creating gaps.

There is a table with groups of redundantly overlapping date intervals where each group has no gaps. Given this table, your job is to find the minimum set of date intervals without creating gaps in the group. A solution is defined such that if you remove any one of the intervals in the solution you will create a gap. The first and last date intervals in a group are considered the anchor intervals and must always be present.

If there are multiple solutions for a group you must choose the one where the number of date intervals is a minimum. If there are still multiple solutions for a group you must choose the one where the sum of the date intervals is a minimum. If there are still multiple solutions for a group you must choose the one based on an order by start date 1, start date 2, etc of the solutions, where 'start date 1' is the lowest start date value of each solution, 'start date 2' is the next lowest start date value of each solution, etc.

"Want to make SQL effortless?
With code-completion, SQL reformatting, script summaries and more, SQL Prompt 5 makes writing, editing, and exploring SQL effortless. Download a 14-day free trial now."

Sample Data

GroupId	StartDate	 EndDate
------- ---------- ----------
1       2012-04-01 2012-04-10 
1       2012-04-05 2012-04-21 
1       2012-04-04 2012-04-20 
1       2012-04-16 2012-04-26 
1       2012-04-15 2012-04-24 
1       2012-04-22 2012-04-30 
2       2012-01-01 2012-01-15 
2       2012-01-05 2012-01-20 
2       2012-01-10 2012-01-24 
2       2012-01-17 2012-01-31

Expected Results

GroupId StartDate  EndDate
------- ---------- ----------
1       2012-04-01 2012-04-10
1       2012-04-04 2012-04-20
1       2012-04-15 2012-04-24
1       2012-04-22 2012-04-30
2       2012-01-01 2012-01-15
2       2012-01-10 2012-01-24
2       2012-01-17 2012-01-31

Rules

  1. Within a group there will be no two date intervals with the same start date or same end date.
  2. Two date intervals in a group where the start date of one equals the end date of the other are considered to be adjacent, i.e. there is no gap between them.
  3. The end date of an interval is always greater than the start date.
  4. The output should be ordered by GroupId, StartDate.

Sample Script

Use the TSQL Script given below to generate the source table and fill them with sample data.

IF OBJECT_ID('TC80','U') IS NOT NULL BEGIN    
    DROP TABLE TC80 
END
GO 
 
CREATE TABLE dbo.TC80 (
    GroupId INT NOT NULL,  
    StartDate DATETIME NOT NULL, 
    EndDate DATETIME NOT NULL
) 
GO 

INSERT TC80(GroupId,StartDate,EndDate)
SELECT 1,'2012-04-01','2012-04-10' UNION ALL
SELECT 1,'2012-04-05','2012-04-21' UNION ALL
SELECT 1,'2012-04-04','2012-04-20' UNION ALL
SELECT 1,'2012-04-16','2012-04-26' UNION ALL
SELECT 1,'2012-04-15','2012-04-24' UNION ALL
SELECT 1,'2012-04-22','2012-04-30' UNION ALL
SELECT 2,'2012-01-01','2012-01-15' UNION ALL
SELECT 2,'2012-01-05','2012-01-20' UNION ALL
SELECT 2,'2012-01-10','2012-01-24' UNION ALL
SELECT 2,'2012-01-17','2012-01-31'

SELECT * FROM TC80

Restrictions

  1. The solution should be a single query that starts with a "SELECT" or “;WITH”

Notes

  1. Read the Submission Guidelines and make sure that your solution follows them.
  2. If you would like to use a Tally Table, you can use the script given here. Your solution should not include the script to create and populate the tally table. You can assume that the tally table will be available in the database where the evaluation team will run your Code.
  3. Your solution can use features from all versions up to and including SQL Server 2012.
"Want to make SQL effortless?
With code-completion, SQL reformatting, script summaries and more, SQL Prompt 5 makes writing, editing, and exploring SQL effortless. Download a 14-day free trial now."

dishdy
16 · 10% · 3269
Submit a Solution
Previous Challenge
Next Challenge
11
Liked
74
Comments
46
Solutions



Submit

TSQL Challenge 80 - Find the minimal set of intervals without creating gaps

Managed Windows Shared Hosting by OrcsWeb

Copyright © Rivera Informatic Private Ltd.