<?xml version="1.0" encoding="UTF-8"?><!-- generator="wordpress/2.2.3" -->
<rss version="2.0" 
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	>
<channel>
	<title>Comments for Webinade - Web Development &amp; Programming Blog</title>
	<link>http://www.webinade.com</link>
	<description>Freshly Squeezed</description>
	<pubDate>Fri, 19 Mar 2010 22:16:03 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.2.3</generator>

	<item>
		<title>Comment on Creating Recursive SQL Calls for Tables with Parent-Child Relationships by mnmdad</title>
		<link>http://www.webinade.com/web-development/creating-recursive-sql-calls-for-tables-with-parent-child-relationships#comment-17111</link>
		<dc:creator>mnmdad</dc:creator>
		<pubDate>Fri, 19 Mar 2010 17:56:22 +0000</pubDate>
		<guid>http://www.webinade.com/web-development/creating-recursive-sql-calls-for-tables-with-parent-child-relationships#comment-17111</guid>
		<description>This is great for recursive expansions, such as BOM and org charts.  What about recursive collapses?  Say I have some intervals:

1,3
2,5
4,6
9,14
10,11
12,13

ultimately, I want the results to be

1,6,3
9,14,3

where the third value is the count of the number of overlapping subintervals composing the interval.

Doing the blind approach leads to a runaway expansion that terminates after a recursive depth of 100.  That makes sense because this syntax is geared toward expanding - that is what the join does.  Actually I suppose it is really the UNION ALL that expands...  

I need to do a cross product of the above, eliminating the duplicates:  the six instances of the interval and itself, e.g. (1,3,1,3) as well as one of (1,3,2,5), (2,5,1,3).  That leaves 15 cross product rows.  Then taking the smallest of the first coordinate and the largest of the second, (1,3,2,5) =&#62; (1,5) for example, and applying the overlap check I would get four rows that look like the original table:  

1,5
2,6
9,14
9,14

Then I need to do the cross product, duplicate elimination, etc. on that set of rows.

Continuing until there are a set of intervals with no overlaps.  So this recursions needs to result in a smaller number of rows with each go-round.

I'll include my code in case anyone wants to try it and supply (I hope) an obvious fix that I just can't see.  It is like I need a set difference function rather than union... and outer joins are not allowed.

TIA!
dc

Code follows:


CREATE TABLE TimeIntervals (
	t0	int not null,
	t1  int not null,
	intcnt int not null
)

INSERT INTO TimeIntervals
(t0, t1, intcnt)
VALUES
(1,3,1)

INSERT INTO TimeIntervals
(t0, t1, intcnt)
VALUES
(2,5,1)

INSERT INTO TimeIntervals
(t0, t1, intcnt)
VALUES
(4,6,1)

INSERT INTO TimeIntervals
(t0, t1, intcnt)
VALUES
(9,14,1)

INSERT INTO TimeIntervals
(t0, t1, intcnt)
VALUES
(10,11,1)

INSERT INTO TimeIntervals
(t0, t1, intcnt)
VALUES
(12,13,1)

         
WITH CollapsedTimeIntervals (t0, t1, intcnt)
AS
(
-- Anchor member definition
select CASE WHEN I.t0 &lt;I&gt; I.s1 THEN I.t1 ELSE I.s1 END AS t1,
       newIntcnt
from (
  select T.t0 AS t0, T.t1 AS t1, S.t0 AS s0, S.t1 AS s1, T.intcnt   1 AS newIntcnt
  from TimeIntervals T, TimeIntervals S
  where T.t0 != S.t0 and T.t1 != S.t1
  and   T.t0 = T.t0 AND S.t0 = T.t0 AND S.t1 = S.t0 AND T.t0 = S.t0 AND T.t1  I.s1 THEN I.t1 ELSE I.s1 END AS t1,
       newIntcnt
from (
  select T.t0 AS t0, T.t1 AS t1, S.t0 AS s0, S.t1 AS s1, T.intcnt   1 AS newIntcnt
  from TimeIntervals T, TimeIntervals S
  where T.t0 != S.t0 and T.t1 != S.t1
  and   T.t0 = T.t0 AND S.t0 = T.t0 AND S.t1 = S.t0 AND T.t0 = S.t0 AND T.t1 </description>
		<content:encoded><![CDATA[<p>This is great for recursive expansions, such as BOM and org charts.  What about recursive collapses?  Say I have some intervals:</p>
<p>1,3<br />
2,5<br />
4,6<br />
9,14<br />
10,11<br />
12,13</p>
<p>ultimately, I want the results to be</p>
<p>1,6,3<br />
9,14,3</p>
<p>where the third value is the count of the number of overlapping subintervals composing the interval.</p>
<p>Doing the blind approach leads to a runaway expansion that terminates after a recursive depth of 100.  That makes sense because this syntax is geared toward expanding - that is what the join does.  Actually I suppose it is really the UNION ALL that expands&#8230;  </p>
<p>I need to do a cross product of the above, eliminating the duplicates:  the six instances of the interval and itself, e.g. (1,3,1,3) as well as one of (1,3,2,5), (2,5,1,3).  That leaves 15 cross product rows.  Then taking the smallest of the first coordinate and the largest of the second, (1,3,2,5) =&gt; (1,5) for example, and applying the overlap check I would get four rows that look like the original table:  </p>
<p>1,5<br />
2,6<br />
9,14<br />
9,14</p>
<p>Then I need to do the cross product, duplicate elimination, etc. on that set of rows.</p>
<p>Continuing until there are a set of intervals with no overlaps.  So this recursions needs to result in a smaller number of rows with each go-round.</p>
<p>I&#8217;ll include my code in case anyone wants to try it and supply (I hope) an obvious fix that I just can&#8217;t see.  It is like I need a set difference function rather than union&#8230; and outer joins are not allowed.</p>
<p>TIA!<br />
dc</p>
<p>Code follows:</p>
<p>CREATE TABLE TimeIntervals (<br />
	t0	int not null,<br />
	t1  int not null,<br />
	intcnt int not null<br />
)</p>
<p>INSERT INTO TimeIntervals<br />
(t0, t1, intcnt)<br />
VALUES<br />
(1,3,1)</p>
<p>INSERT INTO TimeIntervals<br />
(t0, t1, intcnt)<br />
VALUES<br />
(2,5,1)</p>
<p>INSERT INTO TimeIntervals<br />
(t0, t1, intcnt)<br />
VALUES<br />
(4,6,1)</p>
<p>INSERT INTO TimeIntervals<br />
(t0, t1, intcnt)<br />
VALUES<br />
(9,14,1)</p>
<p>INSERT INTO TimeIntervals<br />
(t0, t1, intcnt)<br />
VALUES<br />
(10,11,1)</p>
<p>INSERT INTO TimeIntervals<br />
(t0, t1, intcnt)<br />
VALUES<br />
(12,13,1)</p>
<p>WITH CollapsedTimeIntervals (t0, t1, intcnt)<br />
AS<br />
(<br />
&#8211; Anchor member definition<br />
select CASE WHEN I.t0 <i> I.s1 THEN I.t1 ELSE I.s1 END AS t1,<br />
       newIntcnt<br />
from (<br />
  select T.t0 AS t0, T.t1 AS t1, S.t0 AS s0, S.t1 AS s1, T.intcnt   1 AS newIntcnt<br />
  from TimeIntervals T, TimeIntervals S<br />
  where T.t0 != S.t0 and T.t1 != S.t1<br />
  and   T.t0 = T.t0 AND S.t0 = T.t0 AND S.t1 = S.t0 AND T.t0 = S.t0 AND T.t1  I.s1 THEN I.t1 ELSE I.s1 END AS t1,<br />
       newIntcnt<br />
from (<br />
  select T.t0 AS t0, T.t1 AS t1, S.t0 AS s0, S.t1 AS s1, T.intcnt   1 AS newIntcnt<br />
  from TimeIntervals T, TimeIntervals S<br />
  where T.t0 != S.t0 and T.t1 != S.t1<br />
  and   T.t0 = T.t0 AND S.t0 = T.t0 AND S.t1 = S.t0 AND T.t0 = S.t0 AND T.t1</i></p>
]]></content:encoded>
	</item>
	<item>
		<title>Comment on Creating Recursive SQL Calls for Tables with Parent-Child Relationships by ShepherdAUDRA23</title>
		<link>http://www.webinade.com/web-development/creating-recursive-sql-calls-for-tables-with-parent-child-relationships#comment-17095</link>
		<dc:creator>ShepherdAUDRA23</dc:creator>
		<pubDate>Thu, 18 Mar 2010 04:38:58 +0000</pubDate>
		<guid>http://www.webinade.com/web-development/creating-recursive-sql-calls-for-tables-with-parent-child-relationships#comment-17095</guid>
		<description>According to my analysis, billions of persons on our planet receive the &lt;a href="http://lowest-rate-loans.com" rel="nofollow"&gt;loan&lt;/a&gt; from good banks. Hence, there's good chances to get a credit loan in all countries.</description>
		<content:encoded><![CDATA[<p>According to my analysis, billions of persons on our planet receive the <a href="http://lowest-rate-loans.com" rel="nofollow" onclick="javascript:pageTracker._trackVisit('/outbound/comment/http://lowest-rate-loans.com');">loan</a> from good banks. Hence, there&#8217;s good chances to get a credit loan in all countries.</p>
]]></content:encoded>
	</item>
	<item>
		<title>Comment on Top 10 Sites for Free Stock Photos by ashok pai</title>
		<link>http://www.webinade.com/web-design/top-10-sites-for-free-stock-photos#comment-16993</link>
		<dc:creator>ashok pai</dc:creator>
		<pubDate>Fri, 12 Mar 2010 11:57:02 +0000</pubDate>
		<guid>http://www.webinade.com/web-design/top-10-sites-for-free-stock-photos#comment-16993</guid>
		<description>there's this new site - rgbstock.com. decent place, plenty of photos, various categories.</description>
		<content:encoded><![CDATA[<p>there&#8217;s this new site - rgbstock.com. decent place, plenty of photos, various categories.</p>
]]></content:encoded>
	</item>
	<item>
		<title>Comment on Conditional Assignment Efficiency in PHP by testing`</title>
		<link>http://www.webinade.com/web-development/conditional-assignment-efficiency-in-php#comment-16979</link>
		<dc:creator>testing`</dc:creator>
		<pubDate>Thu, 11 Mar 2010 19:20:20 +0000</pubDate>
		<guid>http://www.webinade.com/web-development/conditional-assignment-efficiency-in-php#comment-16979</guid>
		<description>This is a test. Feel free to delete.</description>
		<content:encoded><![CDATA[<p>This is a test. Feel free to delete.</p>
]]></content:encoded>
	</item>
	<item>
		<title>Comment on Top 10 Sites for Free Stock Photos by Chapel Hill Homes</title>
		<link>http://www.webinade.com/web-design/top-10-sites-for-free-stock-photos#comment-16799</link>
		<dc:creator>Chapel Hill Homes</dc:creator>
		<pubDate>Wed, 10 Mar 2010 11:00:18 +0000</pubDate>
		<guid>http://www.webinade.com/web-design/top-10-sites-for-free-stock-photos#comment-16799</guid>
		<description>These websites for stock photos were brilliant. Thank you so much for sharing this list.</description>
		<content:encoded><![CDATA[<p>These websites for stock photos were brilliant. Thank you so much for sharing this list.</p>
]]></content:encoded>
	</item>
	<item>
		<title>Comment on Creating Recursive SQL Calls for Tables with Parent-Child Relationships by Chapel Hill Houses for Sale</title>
		<link>http://www.webinade.com/web-development/creating-recursive-sql-calls-for-tables-with-parent-child-relationships#comment-16797</link>
		<dc:creator>Chapel Hill Houses for Sale</dc:creator>
		<pubDate>Wed, 10 Mar 2010 10:58:52 +0000</pubDate>
		<guid>http://www.webinade.com/web-development/creating-recursive-sql-calls-for-tables-with-parent-child-relationships#comment-16797</guid>
		<description>If I had to try this from within PHP, it would have made a single exception case where we build an SQL query based on another query, So thanks mate!</description>
		<content:encoded><![CDATA[<p>If I had to try this from within PHP, it would have made a single exception case where we build an SQL query based on another query, So thanks mate!</p>
]]></content:encoded>
	</item>
	<item>
		<title>Comment on Javascript Select All/Check All Checkboxes (Updated) by Chapel Hill Real Estate</title>
		<link>http://www.webinade.com/javascript/javascript-select-allcheck-all-checkboxes#comment-16795</link>
		<dc:creator>Chapel Hill Real Estate</dc:creator>
		<pubDate>Wed, 10 Mar 2010 10:56:48 +0000</pubDate>
		<guid>http://www.webinade.com/javascript/javascript-select-allcheck-all-checkboxes#comment-16795</guid>
		<description>I appreciate you shared the updated java script code. The previous one was not working properly. Thanks.</description>
		<content:encoded><![CDATA[<p>I appreciate you shared the updated java script code. The previous one was not working properly. Thanks.</p>
]]></content:encoded>
	</item>
	<item>
		<title>Comment on Top 10 Sites for Free Stock Photos by Saddles for Sale</title>
		<link>http://www.webinade.com/web-design/top-10-sites-for-free-stock-photos#comment-16792</link>
		<dc:creator>Saddles for Sale</dc:creator>
		<pubDate>Wed, 10 Mar 2010 10:26:15 +0000</pubDate>
		<guid>http://www.webinade.com/web-design/top-10-sites-for-free-stock-photos#comment-16792</guid>
		<description>I was not aware Flickr’s Creative Commons Directory. This was a brilliant website. Thank you for sharing this list.</description>
		<content:encoded><![CDATA[<p>I was not aware Flickr’s Creative Commons Directory. This was a brilliant website. Thank you for sharing this list.</p>
]]></content:encoded>
	</item>
	<item>
		<title>Comment on Creating Recursive SQL Calls for Tables with Parent-Child Relationships by Saddles</title>
		<link>http://www.webinade.com/web-development/creating-recursive-sql-calls-for-tables-with-parent-child-relationships#comment-16791</link>
		<dc:creator>Saddles</dc:creator>
		<pubDate>Wed, 10 Mar 2010 10:24:51 +0000</pubDate>
		<guid>http://www.webinade.com/web-development/creating-recursive-sql-calls-for-tables-with-parent-child-relationships#comment-16791</guid>
		<description>This recursive SQL statement really isn’t too difficult, as there’s only one really confusing bit to it - the actual recursion.</description>
		<content:encoded><![CDATA[<p>This recursive SQL statement really isn’t too difficult, as there’s only one really confusing bit to it - the actual recursion.</p>
]]></content:encoded>
	</item>
	<item>
		<title>Comment on Javascript Select All/Check All Checkboxes (Updated) by Horse Saddles</title>
		<link>http://www.webinade.com/javascript/javascript-select-allcheck-all-checkboxes#comment-16790</link>
		<dc:creator>Horse Saddles</dc:creator>
		<pubDate>Wed, 10 Mar 2010 10:24:11 +0000</pubDate>
		<guid>http://www.webinade.com/javascript/javascript-select-allcheck-all-checkboxes#comment-16790</guid>
		<description>The code worked smooth for me. I appreciate your help!</description>
		<content:encoded><![CDATA[<p>The code worked smooth for me. I appreciate your help!</p>
]]></content:encoded>
	</item>
</channel>
</rss>
