| 
                
                    | 
                            
                                | Author | Topic |  
                                    | tyennStarting Member
 
 
                                        4 Posts | 
                                            
                                            |  Posted - 2008-09-15 : 22:45:01 
 |  
                                            | I have to get the first start date and the last end date for each group of consecutive dates. For exampleid      startdate       enddate--    -------------    ----------1	1/1/2007        1/15/20071       1/16/2007	1/25/20071       1/26/2007	1/30/20071       01/01/2008	08/15/20081       08/16/2008	12/01/20082	2/23/2004	4/18/20042       4/19/2004	08/15/2004should give output asid       startdate       enddate1	01/01/2007	01/31/20071       01/01/2008	12/01/20082	2/23/2004	08/15/2004any help greatly appreciated. |  |  
                                    | visakh16Very Important crosS Applying yaK Herder
 
 
                                    52326 Posts | 
                                        
                                          |  Posted - 2008-09-16 : 00:13:26 
 |  
                                          | [code]SELECT id,min(startdate),max(enddate)FROM(SELECT id,startdate,enddate,dateadd(yy,datediff(yy,0,startdate),0)AS yearstartfrom yourtable)tGROUP BY id,yearstart[/code] |  
                                          |  |  |  
                                    | tyennStarting Member
 
 
                                    4 Posts | 
                                        
                                          |  Posted - 2008-09-16 : 10:52:11 
 |  
                                          | Thanks a lot for your help. There is one problem. If the date range spans more than an year, it is not merging them. If there are two discontinuous periods with in same year, it is merging them.I tried changing your query, but couldn't get it to work. -- create sample table and populate some dateif (object_id('tempdb..#journal') is not null)drop table #journalCREATE TABLE #journal(id int NOT NULL IDENTITY (1, 1),subsid INTEGER NOT NULL,start_date DATETIME NOT NULL,end_date DATETIME NOT NULL)insert #journal (subsid,start_date,end_date)values (1, '1/1/2007', '1/15/2007');insert #journal (subsid,start_date,end_date)values (1, '1/16/2007', '1/25/2007');insert #journal (subsid,start_date,end_date)values (1, '1/26/2007', '1/30/2007');insert #journal (subsid,start_date,end_date)values (1, '1/1/2008', '8/15/2008');insert #journal (subsid,start_date,end_date)values (1, '8/16/2008', '12/1/2008');insert #journal (subsid,start_date,end_date)values (2, '2/23/2004', '4/18/2005');insert #journal (subsid,start_date,end_date)values (2, '4/19/2005', '8/15/2006');insert #journal (subsid,start_date,end_date)values (3, '1/1/2008', '1/15/2008');insert #journal (subsid,start_date,end_date)values (3, '12/1/2008', '12/15/2008');select * from #journal order by start_dateSELECT subsid,min(start_date),max(end_date)FROM(SELECT subsid,start_date,end_date,dateadd(yy,datediff(yy,0,start_date),0)AS yearstartfrom #journal)t GROUP BY subsid,yearstartIt givessubsid startdate enddate1 01/01/2007 01/31/20071 01/01/2008 12/01/20082 2/23/2004 04/18/20052 4/19/2005 08/15/20063 1/1/2008  12/15/2008It works well for id 1. For id 2 and 3 it is giving unexpected resultThanks again.. |  
                                          |  |  |  
                                    | visakh16Very Important crosS Applying yaK Herder
 
 
                                    52326 Posts | 
                                        
                                          |  Posted - 2008-09-16 : 10:55:42 
 |  
                                          | how will you decide which dates form a group? i was assuming grouping should be done based on year. |  
                                          |  |  |  
                                    | SwePesoPatron Saint of Lost Yaks
 
 
                                    30421 Posts | 
                                        
                                          |  Posted - 2008-09-16 : 10:57:50 
 |  
                                          | what about date ranges that span january 1st?do you want to split them?insert #journal (subsid,start_date,end_date)values (1, '12/2/2008', '3/3/2009'); E 12°55'05.63"N 56°04'39.26"
 |  
                                          |  |  |  
                                    | tyennStarting Member
 
 
                                    4 Posts | 
                                        
                                          |  Posted - 2008-09-16 : 11:07:36 
 |  
                                          | even if it spans more than an year, it will be considered as one record. So, we will not split it. This is what we are trying to do:For the same subsid, if the end_date of one record is one day before the start_date of any other record, I have to merge them.Guys, thanks for all your help and time... |  
                                          |  |  |  
                                    | SwePesoPatron Saint of Lost Yaks
 
 
                                    30421 Posts | 
                                        
                                          |  Posted - 2008-09-16 : 11:23:07 
 |  
                                          | Your supplied expected result posted 09/16/2008 :  10:52:11 do not match your sample data. CREATE TABLE	#Journal		(			ID INT IDENTITY (1, 1),			subsID INT NOT NULL,			startDate DATETIME NOT NULL,			endDate DATETIME NOT NULL,			SeqID INT		)SET DATEFORMAT MDYINSERT	#Journal	(		subsID,		startDate,		endDate	)SELECT	1, '1/1/2007', '1/15/2007' UNION ALLSELECT	1, '1/16/2007', '1/25/2007' UNION ALLSELECT	1, '1/26/2007', '1/30/2007' UNION ALLSELECT	1, '1/1/2008', '8/15/2008' UNION ALLSELECT	1, '8/16/2008', '12/1/2008' UNION ALLSELECT	2, '2/23/2004', '4/18/2005' UNION ALLSELECT	2, '4/19/2005', '8/15/2006' UNION ALLSELECT	3, '1/1/2008', '1/15/2008' UNION ALLSELECT	3, '12/1/2008', '12/15/2008'CREATE CLUSTERED INDEX IX_Journal ON #Journal (subsID, startDate, endDate)DECLARE	@SeqID INT,	@subsID INT,	@endDate DATETIMESELECT TOP 1	@SeqID = ID,		@subsID = subsID,		@endDate = DATEADD(DAY, -1, startDate)FROM		#JournalORDER BY	subsID,		startDate,		endDateUPDATE	#JournalSET	@SeqID = SeqID =	CASE					WHEN subsID = @subsID AND DATEADD(DAY, -1, startDate) = @endDate THEN @SeqID					ELSE ID				END,	@endDate = endDate,	@subsID = subsIDSELECT		subsID,		MIN(startDate) AS startDate,		MAX(endDate) AS endDateFROM		#JournalGROUP BY	subsID,		seqIDORDER BY	subsIDDROP TABLE	#Journal E 12°55'05.63"N 56°04'39.26"
 |  
                                          |  |  |  
                                    | tyennStarting Member
 
 
                                    4 Posts | 
                                        
                                          |  Posted - 2008-09-16 : 11:48:55 
 |  
                                          | Thanks a million!! This is exactly what we wanted. It works for all the scenarios. Amazing!! |  
                                          |  |  |  
                                |  |  |  |