<?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: How to find the earliest start date and the latest end date for consecutive transactions in SQL</title>
	<atom:link href="http://oraqa.com/2007/10/29/how-to-find-the-earliest-start-date-and-the-latest-end-date-for-consecutive-transactions-in-sql/feed/" rel="self" type="application/rss+xml" />
	<link>http://oraqa.com/2007/10/29/how-to-find-the-earliest-start-date-and-the-latest-end-date-for-consecutive-transactions-in-sql/</link>
	<description>Oracle Question and Answer</description>
	<lastBuildDate>Wed, 29 Jul 2009 16:01:28 -0600</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: cj</title>
		<link>http://oraqa.com/2007/10/29/how-to-find-the-earliest-start-date-and-the-latest-end-date-for-consecutive-transactions-in-sql/comment-page-1/#comment-225</link>
		<dc:creator>cj</dc:creator>
		<pubDate>Fri, 02 Nov 2007 05:03:10 +0000</pubDate>
		<guid isPermaLink="false">http://oraqa.com/2007/10/29/how-to-find-the-earliest-start-date-and-the-latest-end-date-for-consecutive-transactions-in-sql/#comment-225</guid>
		<description>I like the puzzle.  I had to tackle something similar to this about a year ago.  I had to find the &quot;Maximum Contiguous Minimum Effective Date,&quot; or start date for a member.

I think I had to kludge mine a bit (I&#039;d still like to learn the MODEL clause).  Anyway, here it goes:

&lt;pre&gt;
SELECT
  id,
  min_date,
  max_date
FROM
  (
    SELECT
      id,
      start_date,
      end_date, 
      connect_by_isleaf isleaf,
      level levelof,
      TO_DATE( SUBSTR( SYS_CONNECT_BY_PATH( TO_CHAR( start_date, &#039;MMDDYYYY&#039; ), &#039;-&#039; ), 2, 8 ), &#039;MMDDYYYY&#039; ) min_date,
      ( CASE WHEN CONNECT_BY_ISLEAF = 1 THEN end_date END ) max_date
    FROM
      (
        SELECT
          other_id,
          id,
          start_date,
          end_date,
          ( CASE
              WHEN start_date = LAG( end_date + 1 ) OVER ( PARTITION BY id ORDER BY end_date ) THEN 
                LAG( other_id ) OVER ( PARTITION BY id ORDER BY end_date ) 
            END ) contiguous
        FROM 
          ( 
            SELECT id, rownum other_id, start_date, end_date
            FROM transaction
          )
      )
      START WITH contiguous IS NULL
      CONNECT BY PRIOR other_id = contiguous
  )
WHERE isleaf = 1
  AND levelof &gt; 1;

        ID MIN_DATE  MAX_DATE
---------- --------- ---------
         1 18-JAN-06 27-FEB-06
         1 08-APR-06 17-APR-06
         2 18-JAN-06 27-FEB-06
         2 08-APR-06 18-MAY-06
&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>I like the puzzle.  I had to tackle something similar to this about a year ago.  I had to find the &#8220;Maximum Contiguous Minimum Effective Date,&#8221; or start date for a member.</p>
<p>I think I had to kludge mine a bit (I&#8217;d still like to learn the MODEL clause).  Anyway, here it goes:</p>
<pre>
SELECT
  id,
  min_date,
  max_date
FROM
  (
    SELECT
      id,
      start_date,
      end_date,
      connect_by_isleaf isleaf,
      level levelof,
      TO_DATE( SUBSTR( SYS_CONNECT_BY_PATH( TO_CHAR( start_date, 'MMDDYYYY' ), '-' ), 2, 8 ), 'MMDDYYYY' ) min_date,
      ( CASE WHEN CONNECT_BY_ISLEAF = 1 THEN end_date END ) max_date
    FROM
      (
        SELECT
          other_id,
          id,
          start_date,
          end_date,
          ( CASE
              WHEN start_date = LAG( end_date + 1 ) OVER ( PARTITION BY id ORDER BY end_date ) THEN
                LAG( other_id ) OVER ( PARTITION BY id ORDER BY end_date )
            END ) contiguous
        FROM
          (
            SELECT id, rownum other_id, start_date, end_date
            FROM transaction
          )
      )
      START WITH contiguous IS NULL
      CONNECT BY PRIOR other_id = contiguous
  )
WHERE isleaf = 1
  AND levelof &gt; 1;

        ID MIN_DATE  MAX_DATE
---------- --------- ---------
         1 18-JAN-06 27-FEB-06
         1 08-APR-06 17-APR-06
         2 18-JAN-06 27-FEB-06
         2 08-APR-06 18-MAY-06
</pre>
]]></content:encoded>
	</item>
</channel>
</rss>
