sql server - How to find date ranges in records with consecutive dates and duplicate data -


Maybe there is an easy solution for this, but I can not see it I have a table with a consistent date and often duplicate related statistics for many of these consecutive dates:

  Date Col1 Col2 5/13/2010 1A 5/14/2010 1A 5/15 / 2010 2B 5/16/2010 1A 5/17/2010 1A 5/18/2010 3C 5/19/2010 3C 5/20/2010 3C   

MS T-SQL, I want to find start and end dates for each section of different Col1 and Col2 values:

  Start date end date 1 call 2 5/13/2010 5/14/2010 1 A5 / 15/2010 5/15/2010 2B 5/16/2010 5/17/2010 1A 5/18/2010 5/20/2010 3C  

Perception: Anywhere The date of disappearance is not there Col1 and Col2 are not zero No idea - preferably not using the cursor? For SQL 2005+, I think that should work below

 

> Date with date (COL1, COL2, DATE, DATEADDAY, Select -1 * ROW_NUMBER () (COL1, COL2 depending on order), DATE as YOUR_TABLE from GRP COL1, COL2, MIN (DATE) STARTDATE, MAX (DATE) COL1, COL2 , As the end date from GRP

If you have a duplicate record, DENSE_RANK () Sub-query and one correlation for SQL 2000 instead of ROW_NUMBER ()

Shall include query. Start date (max) (date) COL1, COL2, DATE, as SELECT COL1, COL2, (DATE) and your bootle B from SELECT MIN (DATE), where BDT & gt; = ADT and B.Coll 1 = A.Coll 1 and B.COL2 = A. Call 2 and not existing (Select * Your Tab from C.C.C.L = 1 BCL 1 And CCL 2 = B.Col 2 and DADADFF (DA, BDTET, CDDET) = 1)) GRP by your additive A) GRO1 by COL1, COL2, GRP


Comments