﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / SQL Server 2012 / SQL Server 2012 -  T-SQL  / Find First and Last Period without holes / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Sat, 25 May 2013 12:51:05 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: Find First and Last Period without holes</title><link>http://www.sqlservercentral.com/Forums/Topic1406679-3077-1.aspx</link><description>[quote][b]Sergiy (2/13/2013)[/b][hr][quote][b]dwain.c (1/15/2013)[/b][hr]I understand you already have a solution but this might be a bit more efficient.  The technique is based on an article by Jeff Moden on [url=http://www.sqlservercentral.com/articles/T-SQL/71550/]Grouping Islands of Contiguous Dates[/url] (your periods are like dates).[code="sql"]DECLARE @Test TABLE    ([KEY] VARCHAR(5), Period INT, Data VARCHAR(10))INSERT INTO @TestSELECT 'A',5,'data'UNION ALL SELECT 'A',6,'data'UNION ALL SELECT 'A',8,'data';WITH CTE AS (    SELECT [Key], FirstPeriod=MIN(Period), LastPeriod=MAX(Period)    FROM (        SELECT [KEY], Period, Data            ,n=Period-ROW_NUMBER() OVER (PARTITION BY [KEY] ORDER BY Period)        FROM @Test) a    GROUP BY [Key], n)SELECT b.[KEY], Period, Data, FirstPeriod, LastPeriodFROM CTE aINNER JOIN @Test b ON a.[KEY] = b.[KEY] AND b.Period BETWEEN FirstPeriod AND LastPeriod[/code]Hope this helps![/quote]The script goes nuts if you add another record to the table;[code="sql"]INSERT INTO @TestSELECT 'A',5,'data'UNION ALL SELECT 'A',6,'data'UNION ALL SELECT 'A',6,'other data'UNION ALL SELECT 'A',8,'data'[/code][/quote]I guess that would depend on whether the OPs data contains duplicates on KEY.  In which case I'd agree that this approach won't work.</description><pubDate>Wed, 13 Feb 2013 19:14:21 GMT</pubDate><dc:creator>dwain.c</dc:creator></item><item><title>RE: Find First and Last Period without holes</title><link>http://www.sqlservercentral.com/Forums/Topic1406679-3077-1.aspx</link><description>[quote][b]dwain.c (1/15/2013)[/b][hr]I understand you already have a solution but this might be a bit more efficient.  The technique is based on an article by Jeff Moden on [url=http://www.sqlservercentral.com/articles/T-SQL/71550/]Grouping Islands of Contiguous Dates[/url] (your periods are like dates).[code="sql"]DECLARE @Test TABLE    ([KEY] VARCHAR(5), Period INT, Data VARCHAR(10))INSERT INTO @TestSELECT 'A',5,'data'UNION ALL SELECT 'A',6,'data'UNION ALL SELECT 'A',8,'data';WITH CTE AS (    SELECT [Key], FirstPeriod=MIN(Period), LastPeriod=MAX(Period)    FROM (        SELECT [KEY], Period, Data            ,n=Period-ROW_NUMBER() OVER (PARTITION BY [KEY] ORDER BY Period)        FROM @Test) a    GROUP BY [Key], n)SELECT b.[KEY], Period, Data, FirstPeriod, LastPeriodFROM CTE aINNER JOIN @Test b ON a.[KEY] = b.[KEY] AND b.Period BETWEEN FirstPeriod AND LastPeriod[/code]Hope this helps![/quote]The script goes nuts if you add another record to the table;[code="sql"]INSERT INTO @TestSELECT 'A',5,'data'UNION ALL SELECT 'A',6,'data'UNION ALL SELECT 'A',6,'other data'UNION ALL SELECT 'A',8,'data'[/code]</description><pubDate>Wed, 13 Feb 2013 19:06:16 GMT</pubDate><dc:creator>Sergiy</dc:creator></item><item><title>RE: Find First and Last Period without holes</title><link>http://www.sqlservercentral.com/Forums/Topic1406679-3077-1.aspx</link><description>You can check this as well. Slightly improved version. Is n't it ?DECLARE @Test TABLE    ([KEY] VARCHAR(5), Period INT, Data VARCHAR(10))INSERT INTO @TestSELECT 'A',5,'data'UNION ALL SELECT 'A',6,'data'UNION ALL SELECT 'A',8,'data';WITH CTE AS (    SELECT [Key], Period, Data, FirstPeriod=MIN(Period) OVER (partition by [key], n), LastPeriod=MAX(Period) OVER (partition by [key], n)    FROM (        SELECT [KEY], Period, Data, n=Period-ROW_NUMBER() OVER (PARTITION BY [KEY] ORDER BY Period)        FROM @Test) a        )SELECT [Key], Period, Data, FirstPeriod, LastPeriodFROM CTE a;Thank you.</description><pubDate>Thu, 17 Jan 2013 03:56:04 GMT</pubDate><dc:creator>Bhanu Prakash T</dc:creator></item><item><title>RE: Find First and Last Period without holes</title><link>http://www.sqlservercentral.com/Forums/Topic1406679-3077-1.aspx</link><description>I understand you already have a solution but this might be a bit more efficient.  The technique is based on an article by Jeff Moden on [url=http://www.sqlservercentral.com/articles/T-SQL/71550/]Grouping Islands of Contiguous Dates[/url] (your periods are like dates).[code="sql"]DECLARE @Test TABLE    ([KEY] VARCHAR(5), Period INT, Data VARCHAR(10))INSERT INTO @TestSELECT 'A',5,'data'UNION ALL SELECT 'A',6,'data'UNION ALL SELECT 'A',8,'data';WITH CTE AS (    SELECT [Key], FirstPeriod=MIN(Period), LastPeriod=MAX(Period)    FROM (        SELECT [KEY], Period, Data            ,n=Period-ROW_NUMBER() OVER (PARTITION BY [KEY] ORDER BY Period)        FROM @Test) a    GROUP BY [Key], n)SELECT b.[KEY], Period, Data, FirstPeriod, LastPeriodFROM CTE aINNER JOIN @Test b ON a.[KEY] = b.[KEY] AND b.Period BETWEEN FirstPeriod AND LastPeriod[/code]Hope this helps!</description><pubDate>Tue, 15 Jan 2013 19:30:18 GMT</pubDate><dc:creator>dwain.c</dc:creator></item><item><title>RE: Find First and Last Period without holes</title><link>http://www.sqlservercentral.com/Forums/Topic1406679-3077-1.aspx</link><description>Oh well. I have many many rows with but (relatively) few distinct occurences of KEY (without Period). Using temporary tables solved the performance thing (5 mins to 12 secs). May integrate in some subquery structure again but for now I am fine.Many thanks.</description><pubDate>Mon, 14 Jan 2013 08:10:37 GMT</pubDate><dc:creator>Umfriend</dc:creator></item><item><title>Find First and Last Period without holes</title><link>http://www.sqlservercentral.com/Forums/Topic1406679-3077-1.aspx</link><description>OK, first post and Subject I can not phrase any better.1. I have a table where all records have a field for period.2. For each record I want to know what the first and last periods are in which the period of the current record falls, without missing periods.Assume Table t1:KEY Period Data (Key is three fields + Period)A 5 dataA 6 dataA 8 dataI want a query that yields:KEY Period Data FirstPeriod LastPeriodA 5 data 5 6A 6 data 5 6A 8 data 8 8I have got it but it must be extremely inefficient. I do (for FP (FirstPeriod) only, LP is similar):UPDATE		t1SET		FP =	(				SELECT		MAX(Period)				FROM								(	SELECT t2.KEY, t2.Period							FROM		vAT t2							LEFT JOIN	vAT t3							ON		t2.KEY = t3.KEY							AND		t2.Per	= t3.Per+1							WHERE		t3.Per IS NULL							AND		t1.KEY	= t2.KEY	AND	t1.Per	&amp;gt;= t2.Per							) as tfp					)FROM		vAT t1It does what I need but this can't be right/efficient, can it?Kind rgds,Umf</description><pubDate>Mon, 14 Jan 2013 06:02:12 GMT</pubDate><dc:creator>Umfriend</dc:creator></item></channel></rss>