<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Combining contiguous date ranges in a SQL query &#8211; using CTE recursion</title>
	<atom:link href="http://cdonner.com/combining-contiguous-date-ranges-in-a-sql-query-using-cte-recursion.htm/feed" rel="self" type="application/rss+xml" />
	<link>http://cdonner.com/combining-contiguous-date-ranges-in-a-sql-query-using-cte-recursion.htm</link>
	<description></description>
	<lastBuildDate>Sun, 29 Apr 2012 14:35:11 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
	<item>
		<title>By: Christian Donner</title>
		<link>http://cdonner.com/combining-contiguous-date-ranges-in-a-sql-query-using-cte-recursion.htm/comment-page-1#comment-6079</link>
		<dc:creator>Christian Donner</dc:creator>
		<pubDate>Sat, 12 Feb 2011 19:01:53 +0000</pubDate>
		<guid isPermaLink="false">http://cdonner.com/?p=77#comment-6079</guid>
		<description>Tom, Glad it worked for you.</description>
		<content:encoded><![CDATA[<p>Tom, Glad it worked for you.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: tom</title>
		<link>http://cdonner.com/combining-contiguous-date-ranges-in-a-sql-query-using-cte-recursion.htm/comment-page-1#comment-6077</link>
		<dc:creator>tom</dc:creator>
		<pubDate>Sat, 12 Feb 2011 09:53:47 +0000</pubDate>
		<guid isPermaLink="false">http://cdonner.com/?p=77#comment-6077</guid>
		<description>Aaah silly me problem solved!
The culprit was in the WHERE clause of my query. I needed to put the &quot;last.EmpID = this.EmpID&quot; check into the ON clause instead.
Looking back at the query and the penny dropped - Of course my original query was taking so long, the left outer join itself produces about 50 million rows!!!

Anyway, thanks Chris for your solution again and I should put my thinking cap on first before just blatanly copying code! :)

Cheers!</description>
		<content:encoded><![CDATA[<p>Aaah silly me problem solved!<br />
The culprit was in the WHERE clause of my query. I needed to put the &#8220;last.EmpID = this.EmpID&#8221; check into the ON clause instead.<br />
Looking back at the query and the penny dropped &#8211; Of course my original query was taking so long, the left outer join itself produces about 50 million rows!!!</p>
<p>Anyway, thanks Chris for your solution again and I should put my thinking cap on first before just blatanly copying code! <img src='http://cdonner.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Cheers!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Christian Donner</title>
		<link>http://cdonner.com/combining-contiguous-date-ranges-in-a-sql-query-using-cte-recursion.htm/comment-page-1#comment-6074</link>
		<dc:creator>Christian Donner</dc:creator>
		<pubDate>Fri, 11 Feb 2011 04:02:24 +0000</pubDate>
		<guid isPermaLink="false">http://cdonner.com/?p=77#comment-6074</guid>
		<description>Tom, I would love to, but right now I can&#039;t spare the time and I don&#039;t even know if I would be of any help. I used in recursion in larger databases, but I can&#039;t give you any specific metrics. It&#039;s been years.</description>
		<content:encoded><![CDATA[<p>Tom, I would love to, but right now I can&#8217;t spare the time and I don&#8217;t even know if I would be of any help. I used in recursion in larger databases, but I can&#8217;t give you any specific metrics. It&#8217;s been years.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: tom</title>
		<link>http://cdonner.com/combining-contiguous-date-ranges-in-a-sql-query-using-cte-recursion.htm/comment-page-1#comment-6072</link>
		<dc:creator>tom</dc:creator>
		<pubDate>Thu, 10 Feb 2011 10:33:11 +0000</pubDate>
		<guid isPermaLink="false">http://cdonner.com/?p=77#comment-6072</guid>
		<description>Chris,

You&#039;re probably too busy, but would you mind executing this query if I provided you with the data set I&#039;m working with? It&#039;s about 5 mb csv file.
It&#039;s just that I&#039;m very keen to see why it&#039;s so slow.

Cheers.</description>
		<content:encoded><![CDATA[<p>Chris,</p>
<p>You&#8217;re probably too busy, but would you mind executing this query if I provided you with the data set I&#8217;m working with? It&#8217;s about 5 mb csv file.<br />
It&#8217;s just that I&#8217;m very keen to see why it&#8217;s so slow.</p>
<p>Cheers.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: tom</title>
		<link>http://cdonner.com/combining-contiguous-date-ranges-in-a-sql-query-using-cte-recursion.htm/comment-page-1#comment-6071</link>
		<dc:creator>tom</dc:creator>
		<pubDate>Thu, 10 Feb 2011 10:28:09 +0000</pubDate>
		<guid isPermaLink="false">http://cdonner.com/?p=77#comment-6071</guid>
		<description>Hmmm, I created the indices as you suggested, but it looks like it has no effect on the slowness of the recursion.

Also, I&#039;m now just calling the recursive function with:
SELECT EmpID, StartDate as start, EndDate as [end], BSB,CC, Level
FROM Recursion
without any grouping and ordering just to see if there is any improvement. I noticed that it straight away spits out the first 40 000 to 45000 records quite quickly and then it slows down to a crawl, spitting out only a handful of rows every couple of minutes. And this is just for Level 0. 10 minutes into the query and it is still processing Level 0 records.

Is there anything else I could try? Or does recursion only work for small to medium sets of data?

I&#039;m curious to know, how many records do you have in your data set and what is the performance like?</description>
		<content:encoded><![CDATA[<p>Hmmm, I created the indices as you suggested, but it looks like it has no effect on the slowness of the recursion.</p>
<p>Also, I&#8217;m now just calling the recursive function with:<br />
SELECT EmpID, StartDate as start, EndDate as [end], BSB,CC, Level<br />
FROM Recursion<br />
without any grouping and ordering just to see if there is any improvement. I noticed that it straight away spits out the first 40 000 to 45000 records quite quickly and then it slows down to a crawl, spitting out only a handful of rows every couple of minutes. And this is just for Level 0. 10 minutes into the query and it is still processing Level 0 records.</p>
<p>Is there anything else I could try? Or does recursion only work for small to medium sets of data?</p>
<p>I&#8217;m curious to know, how many records do you have in your data set and what is the performance like?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Christian Donner</title>
		<link>http://cdonner.com/combining-contiguous-date-ranges-in-a-sql-query-using-cte-recursion.htm/comment-page-1#comment-6068</link>
		<dc:creator>Christian Donner</dc:creator>
		<pubDate>Wed, 09 Feb 2011 15:14:40 +0000</pubDate>
		<guid isPermaLink="false">http://cdonner.com/?p=77#comment-6068</guid>
		<description>Start with a composite index on the group by columns:

StartDate, EmpID, BSB, CC

Keep the index on EmpId and Startdate.</description>
		<content:encoded><![CDATA[<p>Start with a composite index on the group by columns:</p>
<p>StartDate, EmpID, BSB, CC</p>
<p>Keep the index on EmpId and Startdate.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: tom</title>
		<link>http://cdonner.com/combining-contiguous-date-ranges-in-a-sql-query-using-cte-recursion.htm/comment-page-1#comment-6066</link>
		<dc:creator>tom</dc:creator>
		<pubDate>Wed, 09 Feb 2011 12:54:18 +0000</pubDate>
		<guid isPermaLink="false">http://cdonner.com/?p=77#comment-6066</guid>
		<description>Also, here is my query:
I had to include an employee id because I have multiple employees. I hope what I have done is correct.

 ;WITH Recursion (EmpID, StartDate, EndDate, BSB, CC, Level)
AS
(
-- Anchor member definition
    SELECT this.EmpID, this.StartDate, this.EndDate, this.BSB, this.CC, 0 AS Level
    FROM TestEmployees AS this
    LEFT OUTER JOIN dbo.TestEmployees AS last
        ON last.EndDate + 1 = this.StartDate 
    WHERE (last.EmpID = this.EmpID AND last.BSB + last.CC  this.BSB + this.CC) or last.BSB + last.CC is null
    UNION ALL
-- Recursive member definition
    SELECT last.EmpID, last.StartDate, this.EndDate, this.BSB, this.CC, Level + 1
    FROM TestEmployees AS this
    inner JOIN Recursion AS last
        ON last.EndDate + 1 = this.StartDate
    WHERE last.EmpID = this.EmpID AND last.BSB + last.CC = this.BSB + this.CC
	and Level&lt;99
)
-- Statement that executes the CTE
SELECT EmpID, StartDate as start, MAX(EndDate) as [end], BSB,CC
FROM Recursion
group by StartDate, EmpID, BSB,CC
order by EmpID, StartDate

I&#039;ve created a clustered index on EmpID and StartDate but the query has been executing for 1hr 45 min!</description>
		<content:encoded><![CDATA[<p>Also, here is my query:<br />
I had to include an employee id because I have multiple employees. I hope what I have done is correct.</p>
<p> ;WITH Recursion (EmpID, StartDate, EndDate, BSB, CC, Level)<br />
AS<br />
(<br />
&#8211; Anchor member definition<br />
    SELECT this.EmpID, this.StartDate, this.EndDate, this.BSB, this.CC, 0 AS Level<br />
    FROM TestEmployees AS this<br />
    LEFT OUTER JOIN dbo.TestEmployees AS last<br />
        ON last.EndDate + 1 = this.StartDate<br />
    WHERE (last.EmpID = this.EmpID AND last.BSB + last.CC  this.BSB + this.CC) or last.BSB + last.CC is null<br />
    UNION ALL<br />
&#8211; Recursive member definition<br />
    SELECT last.EmpID, last.StartDate, this.EndDate, this.BSB, this.CC, Level + 1<br />
    FROM TestEmployees AS this<br />
    inner JOIN Recursion AS last<br />
        ON last.EndDate + 1 = this.StartDate<br />
    WHERE last.EmpID = this.EmpID AND last.BSB + last.CC = this.BSB + this.CC<br />
	and Level&lt;99<br />
)<br />
&#8211; Statement that executes the CTE<br />
SELECT EmpID, StartDate as start, MAX(EndDate) as [end], BSB,CC<br />
FROM Recursion<br />
group by StartDate, EmpID, BSB,CC<br />
order by EmpID, StartDate</p>
<p>I&#039;ve created a clustered index on EmpID and StartDate but the query has been executing for 1hr 45 min!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: tom</title>
		<link>http://cdonner.com/combining-contiguous-date-ranges-in-a-sql-query-using-cte-recursion.htm/comment-page-1#comment-6065</link>
		<dc:creator>tom</dc:creator>
		<pubDate>Wed, 09 Feb 2011 10:45:59 +0000</pubDate>
		<guid isPermaLink="false">http://cdonner.com/?p=77#comment-6065</guid>
		<description>Hi Christian,

Thanks for replying, I actually have no indices on any of the columns. Would I need at least an index on StartDate column?

I really want to use your recursive solution as it works out perfectly, but I can&#039;t justify the 4.5 hours! :)</description>
		<content:encoded><![CDATA[<p>Hi Christian,</p>
<p>Thanks for replying, I actually have no indices on any of the columns. Would I need at least an index on StartDate column?</p>
<p>I really want to use your recursive solution as it works out perfectly, but I can&#8217;t justify the 4.5 hours! <img src='http://cdonner.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Christian Donner</title>
		<link>http://cdonner.com/combining-contiguous-date-ranges-in-a-sql-query-using-cte-recursion.htm/comment-page-1#comment-6064</link>
		<dc:creator>Christian Donner</dc:creator>
		<pubDate>Tue, 08 Feb 2011 14:20:38 +0000</pubDate>
		<guid isPermaLink="false">http://cdonner.com/?p=77#comment-6064</guid>
		<description>This is not normal. I would look at your indices first and see if the right ones are in place.</description>
		<content:encoded><![CDATA[<p>This is not normal. I would look at your indices first and see if the right ones are in place.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: tom</title>
		<link>http://cdonner.com/combining-contiguous-date-ranges-in-a-sql-query-using-cte-recursion.htm/comment-page-1#comment-6063</link>
		<dc:creator>tom</dc:creator>
		<pubDate>Tue, 08 Feb 2011 11:22:27 +0000</pubDate>
		<guid isPermaLink="false">http://cdonner.com/?p=77#comment-6063</guid>
		<description>Hi Christian,
I tried using your solution but it is EXTREMELY slow! I have contiguous date ranges for my employees but I have 450 000 rows in my table. This query took 4.5 hours to complete!!! Is this normal and is there a way to speed it up?

Thanks</description>
		<content:encoded><![CDATA[<p>Hi Christian,<br />
I tried using your solution but it is EXTREMELY slow! I have contiguous date ranges for my employees but I have 450 000 rows in my table. This query took 4.5 hours to complete!!! Is this normal and is there a way to speed it up?</p>
<p>Thanks</p>
]]></content:encoded>
	</item>
</channel>
</rss>

