There's probably any easy solution for this, but I can't see it. I have a table with consecutive dates and often duplicate associated data for several of these consecutive dates:
Date Col1 Col2
5/13/2010 1 A
5/14/2010 1 A
5/15/2010 2 B
5/16/2010 1 A
5/17/2010 1 A
5/18/2010 3 C
5/19/2010 3 C
5/20/2010 3 C
Using MS T-SQL, I wish to find the start and end dates for each run of distinct Col1 and Col2 values:
StartDate EndDate Col1 Col2
5/13/2010 5/14/2010 1 A
5/15/2010 5/15/2010 2 B
5/16/2010 5/17/2010 1 A
5/18/2010 5/20/2010 3 C
Assumptions: There are never any missing dates. Col1 and Col2 are not null. Any ideas - preferably that don't use cursors? Many thanks, -alan
For SQL 2005+ I think the below should work
If you have any duplicate records, use
DENSE_RANK()
instead ofROW_NUMBER()
For SQL 2000 there is a sub query and a co-related query involved.
Here's one approach using
outer apply
. Replace@t
with your table's name.The query first selects the "head" row: rows which start a new group of
col1, col2
. This is done by looking up the "prev"ious row, and saying it must be different in thewhere
clause.Then it searches for the end of the
col1, col2
group. That's a two step process: first search for the the first row of the "next" group, and the row before that is the "last" row.The result of the query matches the example output in your question.