Ačiū labai - pavyko suprasti. Anchoras išduoda viską, kas neturi vėliau einančio periodo, o rekursija jau sukabina likusius. On 2013.09.22 18:44, blah wrote: > toks variantas atrodo veikia gerai: > > WITH CteTable AS ( > SELECT [ELEMENT], [START], [END], [VALUE] > FROM [Test].[dbo].[Table1] t1 > WHERE NOT EXISTS (SELECT 1 FROM [Test].[dbo].[Table1] t2 > WHERE t1.[ELEMENT] = t2.[ELEMENT] AND > t1.[VALUE] = t2.[VALUE] AND DATEADD(DAY, 1, t1.[END]) = t2.[START]) > > UNION ALL > > SELECT t3.[ELEMENT], t3.[START], c.[END], t3.[VALUE] > FROM [Test].[dbo].[Table1] t3 > INNER JOIN CteTable c > ON t3.[ELEMENT] = c.[ELEMENT] AND t3.[VALUE] = c.[VALUE] > AND DATEADD(DAY, 1, t3.[END]) = c.[START] > ), > CteTable2 as ( > select [ELEMENT], MIN([START]) as [START], [END], MAX([VALUE]) as [VALUE] > from CteTable > group by [ELEMENT], [END] > ) > select [ELEMENT], [START], MAX([END]) as [END], MAX([VALUE]) as [VALUE] > from CteTable2 > group by [ELEMENT], [START]