Facebook Sign in | Join

			5 MINUTES to source control your database
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 16 - Find intersections in date ranges and concatenate aggregated labels

Challenge Context

This challenge is derived from a real scenario discussed in a forum. It mainly deals about finding intersections in date ranges.

The original source of data will be a table of persons with their member ship:

                 Description  StartDate   EndDate 	
---------------------------------------------------			
18  Smith  John  Poker Club  01/01/2009  NULL
18  Smith  John  Library     05/01/2009  18/01/2009
18  Smith  John  Gym         10/01/2009  28/01/2009
26  Adams  Jane  Pilates     03/01/2009  16/02/2009

The challenge will be to find each intersection of membership date ranges for each person and produce for this each unique range:

  • a new line with the StartDate and EndDate of the range
  • Concatenate in the description the descriptions of the memberships for this range with:
    • “/” as separator
    • order by membership StartDate

This is the table of results expected:

18  Smith  John  Poker Club                  01/01/2009  04/01/2009
18  Smith  John  Poker Club / Library        05/01/2009  09/01/2009
18  Smith  John  Poker Club / Library / Gym  10/01/2009  18/01/2009
18  Smith  John  Poker Club / Gym            19/01/2009  28/01/2009
18  Smith  John  Poker Club                  29/01/2009  NULL
26  Adams  Jane  Pilates                     03/01/2009  16/02/2009

Note: The headers were removed here for presentation reasons.

Sample Data

Use the following script to generate the sample data used for this challenge:

SET DATEFORMAT dmy
DECLARE @Membership TABLE ( 
	PersonID	int, 
	Surname		nvarchar(16), 
	FirstName	nvarchar(16), 
	Description nvarchar(16), 
	StartDate	datetime, 
	EndDate		datetime)
	
INSERT INTO @Membership VALUES (18, 'Smith', 'John',
 'Poker Club', '01/01/2009', NULL)
INSERT INTO @Membership VALUES (18, 'Smith', 'John', 
'Library', '05/01/2009', '18/01/2009')
INSERT INTO @Membership VALUES (18, 'Smith', 'John', 
'Gym', '10/01/2009', '28/01/2009')
INSERT INTO @Membership VALUES (26, 'Adams', 'Jane', 
'Pilates', '03/01/2009', '16/02/2009')

Notes

  1. Read the Submission Guidelines and make sure that your solution follows them.
  2. The solution should work on SQL Server 2005, 2008 or later versions
  3. Use this forum for any questions related to TSQL Challenge #16

About the Authors.


Share

Comments

# re: TSQL Challenge 16 - Find intersections in date ranges and concatenate aggregated labels

Tuesday, November 03, 2009 1:18 PM by jagatrp

I thik Enddate in line 1 should be 04/30/2009


# re: TSQL Challenge 16 - Find intersections in date ranges and concatenate aggregated labels

Tuesday, November 03, 2009 1:19 PM by jagatrp

I think End date on line 1 should be 04/30/2009??


# re: TSQL Challenge 16 - Find intersections in date ranges and concatenate aggregated labels

Tuesday, November 03, 2009 2:00 PM by Brad Schulz

Please note that all the dates are in DD/MM/YYYY format.  They are NOT in MM/DD/YYYY format.

(Note the SET DATEFORMAT DMY in the script to generate the sample data).

So the end date of 04/01/2009 (January 4th) is correct.


# re: TSQL Challenge 16 - Find intersections in date ranges and concatenate aggregated labels

Thursday, November 05, 2009 6:55 PM by Kevin Suchlicki

Also, if their membership expires and they re-join the next day (i.e. 2 input records for consecutive date ranges), which output would you expect?

18  Smith  John  Poker Club 01/01/2009  03/01/2009

18  Smith  John  Poker Club 04/01/2009  18/01/2009

OR

18  Smith  John  Poker Club 01/01/2009  18/01/2009

By the way, this is a good challenge, thanks!


# re: TSQL Challenge 16 - Find intersections in date ranges and concatenate aggregated labels

Friday, November 06, 2009 7:46 PM by Jacob Sebastian

@ksuchlicki,

I have answered your questions in the forum thread.

thanks

Jacob


Copyright © Rivera Informatic Private Ltd.