<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>MacLochlainns Weblog &#187; NUMTODSINTERVAL</title>
	<atom:link href="http://blog.mclaughlinsoftware.com/tag/numtodsinterval/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.mclaughlinsoftware.com</link>
	<description>Michael McLaughlin's Technical Blog</description>
	<lastBuildDate>Mon, 06 Sep 2010 21:21:48 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Inline views, fabrication, &amp; the WITH clause</title>
		<link>http://blog.mclaughlinsoftware.com/2008/10/19/inline-views-table-fabrication-andthe-with-clause/</link>
		<comments>http://blog.mclaughlinsoftware.com/2008/10/19/inline-views-table-fabrication-andthe-with-clause/#comments</comments>
		<pubDate>Sun, 19 Oct 2008 06:45:51 +0000</pubDate>
		<dc:creator>maclochlainn</dc:creator>
				<category><![CDATA[Oracle]]></category>
		<category><![CDATA[inline views]]></category>
		<category><![CDATA[NUMTODSINTERVAL]]></category>
		<category><![CDATA[SQL table fabrication]]></category>
		<category><![CDATA[SQL WITH clause]]></category>
		<category><![CDATA[table fabrication]]></category>

		<guid isPermaLink="false">http://maclochlainn.wordpress.com/?p=611</guid>
		<description><![CDATA[Sometimes working with a product like Oracle brings a surprise, like a new feature you failed to catch when it was released. I've seen a lot of entries using inline views through the WITH clause in the Oracle forums. It caught my eye because it is such a radical departure from portable SQL syntax of an inline view. I finally went searching to find the rationale for this approach.

The answer doesn't lie with the Charlotte like National Treasure, but with simplifying the join syntax, as qualified in the Oracle Database 2 Day + Data Warehousing Guide 11g, Release 1. The following looks at this approach, and compares using the WITH clause instead of the inline view to perform table fabrication. <a href="http://maclochlainn.wordpress.com/2008/10/19/inline-views-table-fabrication-and-the-with-clause/">[...]</a>]]></description>
			<content:encoded><![CDATA[<p>Sometimes working with a product like Oracle brings a surprise, like a new feature you failed to catch when it was released. I&#8217;ve seen a lot of entries using inline views through the <code>WITH</code> clause in the Oracle forums. It caught my eye because it is such a radical departure from portable SQL syntax of an inline view. I finally went searching to find the rationale for this approach.</p>
<p>The answer doesn&#8217;t lie with the Charlotte like National Treasure, but with simplifying the join syntax, as qualified in the <a title="Oracle Database 2 Day + Data Warehousing Guide 11g" href="http://download.oracle.com/docs/cd/B28359_01/server.111/b28314/tdpdw_sql.htm#sthref262" target="_blank">Oracle Database 2 Day + Data Warehousing Guide 11g, Release 1</a>. The following looks at this approach, and compares using the <code>WITH</code> clause instead of the inline view to perform table fabrication.</p>
<p>Oracle tells us to use the <code>WITH</code> clause when a query has multiple references to the same query block and there are joins and aggregations. The official name of the <code>WITH</code> clause is a subquery factoring clause. Basically, the <code>WITH</code> clause lets you name inline views and then reuse them inside other inline views. This behavior avoids having to reuse the same inline view twice in different parts of a query, which reduces overhead and increases view resolution. Like PL/SQL, they must be defined before they can be referenced. Unlike PL/SQL, they have no equivalent for forward referencing a query block. This feature comes from the ANSI SQL:1999 specification. It is implemented with the same syntax in Microsoft SQL Server 2005 as a Common Table Expression (CTE), but it also happens to be where Microsoft embedded recursive queries.</p>
<p>The basic syntax is:</p>
<p><a href="http://blog.mclaughlinsoftware.com/wp-content/uploads/2008/10/with_clause.png"><img class="aligncenter size-full wp-image-612" style="border:none" title="with_clause" src="http://blog.mclaughlinsoftware.com/wp-content/uploads/2008/10/with_clause.png" alt="" style="border:none" width="474" height="219" /></a></p>
<p>The first code block is assigned the <code>inline</code> name. You can then reuse the <code>inline</code> code block inside any subsequent code block or the master query. The idea is that this syntax is simpler than the generic syntax.</p>
<p>The standard way of writing this is consistent across other SQL databases, and I don&#8217;t really see it as any more complex than the <code>WITH</code> syntax Oracle provides.</p>
<p><a href="http://blog.mclaughlinsoftware.com/wp-content/uploads/2008/10/inlineview.png"><img class="aligncenter size-full wp-image-613" style="border:none" title="inlineview" src="http://blog.mclaughlinsoftware.com/wp-content/uploads/2008/10/inlineview.png" alt="" style="border:none" width="614" height="159" /></a></p>
<p>The <code>WITH</code> clause is also capable of letting you create tables from literal values, which is known as table fabrication. The following syntax uses the with clause to fabricate a table of two columns (<code>x</code> and <code>y</code>) and two rows.</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;">SQL<span style="color: #66cc66;">&gt;</span> <span style="color: #993333; font-weight: bold;">WITH</span> fabricated <span style="color: #993333; font-weight: bold;">AS</span>
  <span style="color: #cc66cc;">2</span>   <span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #cc66cc;">1</span> <span style="color: #993333; font-weight: bold;">AS</span> x<span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">2</span> <span style="color: #993333; font-weight: bold;">AS</span> y <span style="color: #993333; font-weight: bold;">FROM</span> dual
  <span style="color: #cc66cc;">3</span>    <span style="color: #993333; font-weight: bold;">UNION</span> <span style="color: #993333; font-weight: bold;">ALL</span>
  <span style="color: #cc66cc;">4</span>    <span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #cc66cc;">3</span> <span style="color: #993333; font-weight: bold;">AS</span> x<span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">4</span> <span style="color: #993333; font-weight: bold;">AS</span> y <span style="color: #993333; font-weight: bold;">FROM</span> dual<span style="color: #66cc66;">&#41;</span>
  <span style="color: #cc66cc;">5</span>    <span style="color: #993333; font-weight: bold;">SELECT</span> x<span style="color: #66cc66;">,</span> y <span style="color: #993333; font-weight: bold;">FROM</span> fabricated;</pre></div></div>

<p>This produces the following results:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">         X          Y
---------- ----------
         1          2
         3          4</pre></div></div>

<p>The next shows the traditional way of fabricating a table using an inline view:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;">SQL<span style="color: #66cc66;">&gt;</span> <span style="color: #993333; font-weight: bold;">SELECT</span> x<span style="color: #66cc66;">,</span> y
  <span style="color: #cc66cc;">2</span>   <span style="color: #993333; font-weight: bold;">FROM</span> <span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #cc66cc;">1</span> <span style="color: #993333; font-weight: bold;">AS</span> x<span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">2</span> <span style="color: #993333; font-weight: bold;">AS</span> y <span style="color: #993333; font-weight: bold;">FROM</span> dual
  <span style="color: #cc66cc;">3</span>         <span style="color: #993333; font-weight: bold;">UNION</span> <span style="color: #993333; font-weight: bold;">ALL</span>
  <span style="color: #cc66cc;">4</span>         <span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #cc66cc;">3</span> <span style="color: #993333; font-weight: bold;">AS</span> x<span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">4</span> <span style="color: #993333; font-weight: bold;">AS</span> y <span style="color: #993333; font-weight: bold;">FROM</span> dual<span style="color: #66cc66;">&#41;</span> fabricated;</pre></div></div>

<p>This also produces the same results as before, two rows of <code>X</code> and <code>Y</code> variables.</p>
<p>You can also use this type of syntax in MySQL to fabricate a table. You can&#8217;t use the <code>WITH</code> clause in MySQL because it&#8217;s not supported. You&#8217;ll notice in the example that the <code>FROM dual</code> portion is omitted in MySQL. Wouldn&#8217;t it be nice if Oracle let that happen too?</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;">SQL<span style="color: #66cc66;">&gt;</span> <span style="color: #993333; font-weight: bold;">SELECT</span> x<span style="color: #66cc66;">,</span> y
  <span style="color: #cc66cc;">2</span>  <span style="color: #993333; font-weight: bold;">FROM</span>  <span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #cc66cc;">1</span> <span style="color: #993333; font-weight: bold;">AS</span> x<span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">2</span> <span style="color: #993333; font-weight: bold;">AS</span> y
  <span style="color: #cc66cc;">3</span>         <span style="color: #993333; font-weight: bold;">UNION</span> <span style="color: #993333; font-weight: bold;">ALL</span>
  <span style="color: #cc66cc;">4</span>         <span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #cc66cc;">3</span> <span style="color: #993333; font-weight: bold;">AS</span> x<span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">4</span> <span style="color: #993333; font-weight: bold;">AS</span> y<span style="color: #66cc66;">&#41;</span> fabricated;</pre></div></div>

<p><a name="NUMTODSINTERVAL"></a><br />
A neat function that I picked up on the Oracle Technical Network is the <code>NUMTODSINTERVAL</code> (number to date-stamp interval) function, which can create intervals for qualifying sales by hour or quarter hour. More or less it is a way to fabricate timing intervals. Here&#8217;s a quick example:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;">SQL<span style="color: #66cc66;">&gt;</span> <span style="color: #993333; font-weight: bold;">SELECT</span>   TO_CHAR<span style="color: #66cc66;">&#40;</span>SYSDATE <span style="color: #66cc66;">-</span> NUMTODSINTERVAL<span style="color: #66cc66;">&#40;</span>dist<span style="color: #66cc66;">,</span><span style="color: #ff0000;">'HOUR'</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #cc66cc;">2</span>                  <span style="color: #66cc66;">,</span><span style="color: #ff0000;">'DD-MON-YYYY HH24:MI:SS'</span><span style="color: #66cc66;">&#41;</span> bracket
  <span style="color: #cc66cc;">3</span>  <span style="color: #993333; font-weight: bold;">FROM</span>    <span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #cc66cc;">1</span> <span style="color: #993333; font-weight: bold;">AS</span> dist <span style="color: #993333; font-weight: bold;">FROM</span> dual
  <span style="color: #cc66cc;">4</span>           <span style="color: #993333; font-weight: bold;">UNION</span> <span style="color: #993333; font-weight: bold;">ALL</span>
  <span style="color: #cc66cc;">5</span>           <span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #cc66cc;">2</span> <span style="color: #993333; font-weight: bold;">AS</span> dist <span style="color: #993333; font-weight: bold;">FROM</span> dual
  <span style="color: #cc66cc;">6</span>           <span style="color: #993333; font-weight: bold;">UNION</span> <span style="color: #993333; font-weight: bold;">ALL</span>
  <span style="color: #cc66cc;">7</span>           <span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #cc66cc;">3</span> <span style="color: #993333; font-weight: bold;">AS</span> dist <span style="color: #993333; font-weight: bold;">FROM</span> dual<span style="color: #66cc66;">&#41;</span> fabricated;</pre></div></div>

<p>The output is:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">BRACKET
-------------------
22-OCT-2008 23:07:15
22-OCT-2008 22:07:15
22-OCT-2008 21:07:15</pre></div></div>

<p>This has been the syntax, now I&#8217;ll have to check whether there are any performance differences. I suspect that since the execution plan is the same that there aren&#8217;t any performance differences but you never know until you test it.<br />
<a name="unsupported"></a><br />
More or less they were but I tripped into a performance shortfall of the <code>WITH</code> clause. It was a complete accident when I was trying to convert a MySQL SQL syntax model into Oracle SQL. The smaller change to the code was to use a WITH clause but I found it didn&#8217;t work.</p>
<p>You can&#8217;t use the <code>WITH</code> clause inside a subquery for a multiple row insert. It raises an <code>ORA-32034</code> error if you attempt it, which struck me as bizare. A normal inline view works fine but the <code>WITH</code> clause doesn&#8217;t.</p>
<p>Here&#8217;s a simple example of embedding an inline view into an <code>INSERT</code> statement. It works seamlessly in Oracle 11g:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> contact_copy
<span style="color: #66cc66;">&#40;</span> <span style="color: #993333; font-weight: bold;">SELECT</span>   contact_s1<span style="color: #66cc66;">.</span><span style="color: #993333; font-weight: bold;">NEXTVAL</span>
  <span style="color: #66cc66;">,</span>        <span style="color: #cc66cc;">1001</span>
  <span style="color: #66cc66;">,</span>        cl<span style="color: #66cc66;">.</span>contact_type
  <span style="color: #66cc66;">,</span>        <span style="color: #ff0000;">'Doe'</span>
  <span style="color: #66cc66;">,</span>        <span style="color: #ff0000;">'John'</span>
  <span style="color: #66cc66;">,</span>        <span style="color: #993333; font-weight: bold;">NULL</span>
  <span style="color: #66cc66;">,</span>        <span style="color: #cc66cc;">3</span>
  <span style="color: #66cc66;">,</span>        SYSDATE
  <span style="color: #66cc66;">,</span>        <span style="color: #cc66cc;">3</span>
  <span style="color: #66cc66;">,</span>        SYSDATE
  <span style="color: #993333; font-weight: bold;">FROM</span>     dual
  <span style="color: #993333; font-weight: bold;">CROSS</span> <span style="color: #993333; font-weight: bold;">JOIN</span>
 <span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span>   common_lookup_id <span style="color: #993333; font-weight: bold;">AS</span> contact_type
  <span style="color: #993333; font-weight: bold;">FROM</span>     common_lookup
  <span style="color: #993333; font-weight: bold;">WHERE</span>    common_lookup_type <span style="color: #993333; font-weight: bold;">LIKE</span> <span style="color: #ff0000;">'%DAY RENTAL'</span><span style="color: #66cc66;">&#41;</span> cl <span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>When I switched to what appeared as the equivalent syntax using a <code>WITH</code> clause, it failed and raised the <code>ORA-32034: unsupported use of with clause</code> error. The following shows you how the <code>WITH</code> would be used, if it could be used:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> contact_copy
<span style="color: #66cc66;">&#40;</span> <span style="color: #993333; font-weight: bold;">WITH</span>  cl <span style="color: #993333; font-weight: bold;">AS</span>
 <span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span>   common_lookup_id <span style="color: #993333; font-weight: bold;">AS</span> contact_type
  <span style="color: #993333; font-weight: bold;">FROM</span>     common_lookup
  <span style="color: #993333; font-weight: bold;">WHERE</span>    common_lookup_type <span style="color: #993333; font-weight: bold;">LIKE</span> <span style="color: #ff0000;">'%DAY RENTAL'</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #993333; font-weight: bold;">SELECT</span>   contact_s1<span style="color: #66cc66;">.</span><span style="color: #993333; font-weight: bold;">NEXTVAL</span>
  <span style="color: #66cc66;">,</span>        <span style="color: #cc66cc;">1001</span>
  <span style="color: #66cc66;">,</span>        cl<span style="color: #66cc66;">.</span>contact_type
  <span style="color: #66cc66;">,</span>        <span style="color: #ff0000;">'Doe'</span>
  <span style="color: #66cc66;">,</span>        <span style="color: #ff0000;">'John'</span>
  <span style="color: #66cc66;">,</span>        <span style="color: #993333; font-weight: bold;">NULL</span>
  <span style="color: #66cc66;">,</span>        <span style="color: #cc66cc;">3</span>
  <span style="color: #66cc66;">,</span>        SYSDATE
  <span style="color: #66cc66;">,</span>        <span style="color: #cc66cc;">3</span>
  <span style="color: #66cc66;">,</span>        SYSDATE
  <span style="color: #993333; font-weight: bold;">FROM</span>     dual <span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>I thought that might be the reason why Oracle didn&#8217;t bother putting it in the SQL reference manual for Oracle Database 10g or 11g. However, Dominic Brooks provided the correct syntax. Very interesting that you simply start with the <code>WITH</code> clause and exclude the enclosing parentheses. Quite a departure from the normal syntax for a multiple row insert.</p>
<p>The correct syntax is when the <code>cl</code> subquery returns one row is like Dominic&#8217;s:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> contact_copy
<span style="color: #993333; font-weight: bold;">WITH</span>  cl <span style="color: #993333; font-weight: bold;">AS</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span>   common_lookup_id <span style="color: #993333; font-weight: bold;">AS</span> contact_type
 <span style="color: #993333; font-weight: bold;">FROM</span>     common_lookup
 <span style="color: #993333; font-weight: bold;">WHERE</span>    common_lookup_type <span style="color: #993333; font-weight: bold;">LIKE</span> <span style="color: #ff0000;">'%DAY RENTAL'</span><span style="color: #66cc66;">&#41;</span>
 <span style="color: #993333; font-weight: bold;">SELECT</span>   contact_s1<span style="color: #66cc66;">.</span><span style="color: #993333; font-weight: bold;">NEXTVAL</span>
 <span style="color: #66cc66;">,</span>        <span style="color: #cc66cc;">1001</span>
 <span style="color: #66cc66;">,</span>       <span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span> cl<span style="color: #66cc66;">.</span>contact_type <span style="color: #993333; font-weight: bold;">FROM</span> cl<span style="color: #66cc66;">&#41;</span>
 <span style="color: #66cc66;">,</span>        <span style="color: #ff0000;">'Doe'</span>
 <span style="color: #66cc66;">,</span>        <span style="color: #ff0000;">'John'</span>
 <span style="color: #66cc66;">,</span>        <span style="color: #993333; font-weight: bold;">NULL</span>
 <span style="color: #66cc66;">,</span>        <span style="color: #cc66cc;">3</span>
 <span style="color: #66cc66;">,</span>        SYSDATE
 <span style="color: #66cc66;">,</span>        <span style="color: #cc66cc;">3</span>
 <span style="color: #66cc66;">,</span>        SYSDATE
 <span style="color: #993333; font-weight: bold;">FROM</span>     dual;</pre></div></div>

<p>The correct syntax is when the <code>cl</code> subquery returns more than one row differs from Dominic&#8217;s. You need a <code>CROSS JOIN</code> to multiply the other static values by the number of rows returned by the subquery so that you have a multiple row insert statement.</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> contact_copy
<span style="color: #993333; font-weight: bold;">WITH</span>  cl <span style="color: #993333; font-weight: bold;">AS</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span>   common_lookup_id <span style="color: #993333; font-weight: bold;">AS</span> contact_type
 <span style="color: #993333; font-weight: bold;">FROM</span>     common_lookup
 <span style="color: #993333; font-weight: bold;">WHERE</span>    common_lookup_type <span style="color: #993333; font-weight: bold;">LIKE</span> <span style="color: #ff0000;">'%DAY RENTAL'</span><span style="color: #66cc66;">&#41;</span>
 <span style="color: #993333; font-weight: bold;">SELECT</span>   contact_s1<span style="color: #66cc66;">.</span><span style="color: #993333; font-weight: bold;">NEXTVAL</span>
 <span style="color: #66cc66;">,</span>        <span style="color: #cc66cc;">1001</span>
 <span style="color: #66cc66;">,</span>        cl<span style="color: #66cc66;">.</span>contact_type
 <span style="color: #66cc66;">,</span>        <span style="color: #ff0000;">'Doe'</span>
 <span style="color: #66cc66;">,</span>        <span style="color: #ff0000;">'John'</span>
 <span style="color: #66cc66;">,</span>        <span style="color: #993333; font-weight: bold;">NULL</span>
 <span style="color: #66cc66;">,</span>        <span style="color: #cc66cc;">3</span>
 <span style="color: #66cc66;">,</span>        SYSDATE
 <span style="color: #66cc66;">,</span>        <span style="color: #cc66cc;">3</span>
 <span style="color: #66cc66;">,</span>        SYSDATE
 <span style="color: #993333; font-weight: bold;">FROM</span>     dual <span style="color: #993333; font-weight: bold;">CROSS</span> <span style="color: #993333; font-weight: bold;">JOIN</span> cl;</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://blog.mclaughlinsoftware.com/2008/10/19/inline-views-table-fabrication-andthe-with-clause/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
