<?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 Hierarchical Queries With Analytics</title>
	<atom:link href="http://www.ardentperf.com/2007/05/18/combining-hierarchical-queries-with-analytics/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.ardentperf.com/2007/05/18/combining-hierarchical-queries-with-analytics/</link>
	<description>Jeremy's Oracle Resources and Ramblings</description>
	<lastBuildDate>Wed, 03 Mar 2010 03:35:43 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Accessing Arbitrary Ancestors in Hierarchical Queries : Ardent Performance Computing</title>
		<link>http://www.ardentperf.com/2007/05/18/combining-hierarchical-queries-with-analytics/comment-page-1/#comment-221</link>
		<dc:creator>Accessing Arbitrary Ancestors in Hierarchical Queries : Ardent Performance Computing</dc:creator>
		<pubDate>Wed, 23 May 2007 15:59:06 +0000</pubDate>
		<guid isPermaLink="false">http://www.ardentperf.com/2007/05/18/combining-hierarchical-queries-with-analytics/#comment-221</guid>
		<description>[...] Friday I wrote about a client query that could be rewritten in a much more optimized manner. However rewriting it was a little tricky, involving a hierarchical query and a column that needed [...]</description>
		<content:encoded><![CDATA[<p>[...] Friday I wrote about a client query that could be rewritten in a much more optimized manner. However rewriting it was a little tricky, involving a hierarchical query and a column that needed [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jeremy</title>
		<link>http://www.ardentperf.com/2007/05/18/combining-hierarchical-queries-with-analytics/comment-page-1/#comment-179</link>
		<dc:creator>Jeremy</dc:creator>
		<pubDate>Fri, 18 May 2007 19:06:58 +0000</pubDate>
		<guid isPermaLink="false">http://www.ardentperf.com/2007/05/18/combining-hierarchical-queries-with-analytics/#comment-179</guid>
		<description>Aaah -- I bet that the &#039;&lt;&#039; was messing you up.

If you embed your code inside &lt;pre&gt; and &lt;code&gt; then it will work and will be formatted better too.  You can also use the &quot;Preview&quot; button to check that it works.

Now that I see your whole query I could run it - very cool!  I took a very similar approach to you; I&#039;ll post what I did in a few days just in case anyone else is thinking of posting.  Only thing is that the VP_NAME column is wrong for everyone under JONES... also, KING lists himself as a VP whereas in the problem statement there is no VP listed for KING.  But you got the salary part which I thought was the hardest.

One other thing - I was able to do it without using sys_connect_by_path...  ;)

&lt;pre&gt;&lt;code&gt;SQL&gt; run
  1  select --rm,
  2         --lvl,
  3         ename,
  4         empno,
  5         first_value(no_space_ename) over (partition by vp_num) vp_name,
  6         sal,
  7         (
  8            select sum(
  9                        to_number (
 10                          substr (sal_str,
 11                            instr(sal_str , &#039;+&#039;, 1, rownum ) + 1,
 12                            instr(sal_str, &#039;+&#039;, 1, rownum + 1) -
 13                              instr(sal_str , &#039;+&#039;, 1, rownum ) - 1
 14                              ))
 15                      )
 16            from dual
 17            connect by level &lt;=
 18                      length(
 19                        translate (q1.sal_str, chr(9)&#124;&#124;&#039;0123456789&#039;, &#039; &#039;))  -1
 20         ) sal_sum
 21  from
 22  (
 23  select rm,
 24         lvl,
 25         ename,
 26         empno,
 27         no_space_ename,
 28         sal_str,
 29         max(vp_rm) over (order by rownum) vp_num,
 30         sal
 31  from
 32  (
 33  select rownum rm,
 34         level lvl,
 35         sys_connect_by_path(sal, &#039;+&#039;)&#124;&#124;&#039;+&#039; sal_str,
 36         decode(level, 2, rownum, null) vp_rm,
 37         lpad(ename, length(ename) + level, &#039; &#039;) ename,
 38         ename no_space_ename,
 39         empno,
 40         sal
 41  from emp
 42  start with mgr is null
 43  connect by mgr = prior empno
 44  )
 45  ) q1
 46* order by rm

ENAME                EMPNO VP_NAME           SAL    SAL_SUM
--------------- ---------- ---------- ---------- ----------
 KING                 7839 KING             5000       5000
  JONES               7566 SMITH            2975       7975
   SCOTT              7788 SMITH            3000      10975
    ADAMS             7876 SMITH            1100      12075
   FORD               7902 SMITH            3000      10975
    SMITH             7369 SMITH             800      11775
  BLAKE               7698 BLAKE            2850       7850
   ALLEN              7499 BLAKE            1600       9450
   WARD               7521 BLAKE            1250       9100
   MARTIN             7654 BLAKE            1250       9100
   TURNER             7844 BLAKE            1500       9350
   JAMES              7900 BLAKE             950       8800
  CLARK               7782 CLARK            2450       7450
   MILLER             7934 CLARK            1300       8750

14 rows selected.
&lt;/code&gt;&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>Aaah &#8212; I bet that the &#8216;&lt;&#8217; was messing you up.</p>
<p>If you embed your code inside &lt;pre&gt; and &lt;code&gt; then it will work and will be formatted better too.  You can also use the &#8220;Preview&#8221; button to check that it works.</p>
<p>Now that I see your whole query I could run it &#8211; very cool!  I took a very similar approach to you; I&#8217;ll post what I did in a few days just in case anyone else is thinking of posting.  Only thing is that the VP_NAME column is wrong for everyone under JONES&#8230; also, KING lists himself as a VP whereas in the problem statement there is no VP listed for KING.  But you got the salary part which I thought was the hardest.</p>
<p>One other thing &#8211; I was able to do it without using sys_connect_by_path&#8230;  ;)</p>
<pre><code>SQL> run
  1  select --rm,
  2         --lvl,
  3         ename,
  4         empno,
  5         first_value(no_space_ename) over (partition by vp_num) vp_name,
  6         sal,
  7         (
  8            select sum(
  9                        to_number (
 10                          substr (sal_str,
 11                            instr(sal_str , '+', 1, rownum ) + 1,
 12                            instr(sal_str, '+', 1, rownum + 1) -
 13                              instr(sal_str , '+', 1, rownum ) - 1
 14                              ))
 15                      )
 16            from dual
 17            connect by level < =
 18                      length(
 19                        translate (q1.sal_str, chr(9)||'0123456789', ' '))  -1
 20         ) sal_sum
 21  from
 22  (
 23  select rm,
 24         lvl,
 25         ename,
 26         empno,
 27         no_space_ename,
 28         sal_str,
 29         max(vp_rm) over (order by rownum) vp_num,
 30         sal
 31  from
 32  (
 33  select rownum rm,
 34         level lvl,
 35         sys_connect_by_path(sal, '+')||'+' sal_str,
 36         decode(level, 2, rownum, null) vp_rm,
 37         lpad(ename, length(ename) + level, ' ') ename,
 38         ename no_space_ename,
 39         empno,
 40         sal
 41  from emp
 42  start with mgr is null
 43  connect by mgr = prior empno
 44  )
 45  ) q1
 46* order by rm

ENAME                EMPNO VP_NAME           SAL    SAL_SUM
--------------- ---------- ---------- ---------- ----------
 KING                 7839 KING             5000       5000
  JONES               7566 SMITH            2975       7975
   SCOTT              7788 SMITH            3000      10975
    ADAMS             7876 SMITH            1100      12075
   FORD               7902 SMITH            3000      10975
    SMITH             7369 SMITH             800      11775
  BLAKE               7698 BLAKE            2850       7850
   ALLEN              7499 BLAKE            1600       9450
   WARD               7521 BLAKE            1250       9100
   MARTIN             7654 BLAKE            1250       9100
   TURNER             7844 BLAKE            1500       9350
   JAMES              7900 BLAKE             950       8800
  CLARK               7782 CLARK            2450       7450
   MILLER             7934 CLARK            1300       8750

14 rows selected.
</code></code></pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Raj</title>
		<link>http://www.ardentperf.com/2007/05/18/combining-hierarchical-queries-with-analytics/comment-page-1/#comment-178</link>
		<dc:creator>Raj</dc:creator>
		<pubDate>Fri, 18 May 2007 18:56:35 +0000</pubDate>
		<guid isPermaLink="false">http://www.ardentperf.com/2007/05/18/combining-hierarchical-queries-with-analytics/#comment-178</guid>
		<description>Hi

I have pasted the complete query in the following blog.

http://rajplsql.blogspot.com/

Please share your thoughts on the same.

Thanks
Raj</description>
		<content:encoded><![CDATA[<p>Hi</p>
<p>I have pasted the complete query in the following blog.</p>
<p><a href="http://rajplsql.blogspot.com/" rel="nofollow">http://rajplsql.blogspot.com/</a></p>
<p>Please share your thoughts on the same.</p>
<p>Thanks<br />
Raj</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Raj</title>
		<link>http://www.ardentperf.com/2007/05/18/combining-hierarchical-queries-with-analytics/comment-page-1/#comment-175</link>
		<dc:creator>Raj</dc:creator>
		<pubDate>Fri, 18 May 2007 18:12:40 +0000</pubDate>
		<guid isPermaLink="false">http://www.ardentperf.com/2007/05/18/combining-hierarchical-queries-with-analytics/#comment-175</guid>
		<description>Why posts are not visible, my ie7 says there is some runtime error &quot;Unknown runtime error!&quot;.

Please let me know if i can mail the query.

Yes, I do have selected from EMP table.

My inner most query:
SELECT ROWNUM rm,
       LEVEL lvl,
       sys_connect_by_path(sal, &#039;+&#039;)&#124;&#124;&#039;+&#039; sal_str,
       decode(level, 2, rownum, null) vp_rm,
       LPAD(ename, length(ename) + level, &#039; &#039;) ename,
       ename no_space_ename,
       empno,
       sal
FROM emp
START WITH mgr IS NULL
CONNECT BY mgr = PRIOR empno


Thanks
Raj</description>
		<content:encoded><![CDATA[<p>Why posts are not visible, my ie7 says there is some runtime error &#8220;Unknown runtime error!&#8221;.</p>
<p>Please let me know if i can mail the query.</p>
<p>Yes, I do have selected from EMP table.</p>
<p>My inner most query:<br />
SELECT ROWNUM rm,<br />
       LEVEL lvl,<br />
       sys_connect_by_path(sal, &#8216;+&#8217;)||&#8217;+&#8217; sal_str,<br />
       decode(level, 2, rownum, null) vp_rm,<br />
       LPAD(ename, length(ename) + level, &#8216; &#8216;) ename,<br />
       ename no_space_ename,<br />
       empno,<br />
       sal<br />
FROM emp<br />
START WITH mgr IS NULL<br />
CONNECT BY mgr = PRIOR empno</p>
<p>Thanks<br />
Raj</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Raj</title>
		<link>http://www.ardentperf.com/2007/05/18/combining-hierarchical-queries-with-analytics/comment-page-1/#comment-172</link>
		<dc:creator>Raj</dc:creator>
		<pubDate>Fri, 18 May 2007 18:03:29 +0000</pubDate>
		<guid isPermaLink="false">http://www.ardentperf.com/2007/05/18/combining-hierarchical-queries-with-analytics/#comment-172</guid>
		<description>My query is not fitting completly, i am posting in 3 parts:

Part1:
SELECT rm,
       lvl,
       ename,
       empno,
       sal_str,
       first_value(no_space_ename) OVER (PARTITION BY vp_num) vp_name,
       sal,
       (
          SELECT sum( 
                      to_number (
                        SUBSTR (sal_str, 
                          INSTR(sal_str , &#039;+&#039;, 1, ROWNUM ) + 1, 
                          INSTR(sal_str, &#039;+&#039;, 1, ROWNUM + 1) - 
                            INSTR(sal_str , &#039;+&#039;, 1, ROWNUM ) - 1
                            ))
                    )
          FROM dual
          CONNECT BY LEVEL </description>
		<content:encoded><![CDATA[<p>My query is not fitting completly, i am posting in 3 parts:</p>
<p>Part1:<br />
SELECT rm,<br />
       lvl,<br />
       ename,<br />
       empno,<br />
       sal_str,<br />
       first_value(no_space_ename) OVER (PARTITION BY vp_num) vp_name,<br />
       sal,<br />
       (<br />
          SELECT sum(<br />
                      to_number (<br />
                        SUBSTR (sal_str,<br />
                          INSTR(sal_str , &#8216;+&#8217;, 1, ROWNUM ) + 1,<br />
                          INSTR(sal_str, &#8216;+&#8217;, 1, ROWNUM + 1) &#8211;<br />
                            INSTR(sal_str , &#8216;+&#8217;, 1, ROWNUM ) &#8211; 1<br />
                            ))<br />
                    )<br />
          FROM dual<br />
          CONNECT BY LEVEL</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jeremy</title>
		<link>http://www.ardentperf.com/2007/05/18/combining-hierarchical-queries-with-analytics/comment-page-1/#comment-171</link>
		<dc:creator>Jeremy</dc:creator>
		<pubDate>Fri, 18 May 2007 17:46:27 +0000</pubDate>
		<guid isPermaLink="false">http://www.ardentperf.com/2007/05/18/combining-hierarchical-queries-with-analytics/#comment-171</guid>
		<description>hmmm... i think there might be a typo in there somewhere...

&lt;pre&gt;&lt;code&gt;...
  9  SELECT sum( to_number (SUBSTR (sal_str,
 10    INSTR(sal_str , &#039;+&#039;, 1, ROWNUM ) + 1,
 11    INSTR(sal_str, &#039;+&#039;, 1, ROWNUM + 1)
 12      - INSTR(sal_str , &#039;+&#039;, 1, ROWNUM ) - 1)))
 13  FROM dual
 14* CONNECT BY LEVEL
first_value(no_space_ename) OVER (PARTITION BY vp_num) vp_name,
*
ERROR at line 6:
ORA-00921: unexpected end of SQL command
&lt;/code&gt;&lt;/pre&gt;

also, it might be good to access the EMP table at some point since that&#039;s where the data is...  good start though - keep trying!

&lt;em&gt;Update: Wordpress apparently thought your last comment was spam until I found it just now...  so to answer your question, if you click &quot;Home&quot; and then &quot;Contact Info&quot; you will see my email addresses.  Drop me a line anytime!&lt;/em&gt;</description>
		<content:encoded><![CDATA[<p>hmmm&#8230; i think there might be a typo in there somewhere&#8230;</p>
<pre><code>...
  9  SELECT sum( to_number (SUBSTR (sal_str,
 10    INSTR(sal_str , '+', 1, ROWNUM ) + 1,
 11    INSTR(sal_str, '+', 1, ROWNUM + 1)
 12      - INSTR(sal_str , '+', 1, ROWNUM ) - 1)))
 13  FROM dual
 14* CONNECT BY LEVEL
first_value(no_space_ename) OVER (PARTITION BY vp_num) vp_name,
*
ERROR at line 6:
ORA-00921: unexpected end of SQL command
</code></pre>
<p>also, it might be good to access the EMP table at some point since that&#8217;s where the data is&#8230;  good start though &#8211; keep trying!</p>
<p><em>Update: Wordpress apparently thought your last comment was spam until I found it just now&#8230;  so to answer your question, if you click &#8220;Home&#8221; and then &#8220;Contact Info&#8221; you will see my email addresses.  Drop me a line anytime!</em></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Raj</title>
		<link>http://www.ardentperf.com/2007/05/18/combining-hierarchical-queries-with-analytics/comment-page-1/#comment-170</link>
		<dc:creator>Raj</dc:creator>
		<pubDate>Fri, 18 May 2007 16:52:17 +0000</pubDate>
		<guid isPermaLink="false">http://www.ardentperf.com/2007/05/18/combining-hierarchical-queries-with-analytics/#comment-170</guid>
		<description>Hi,
Query is not fitting in the comments box, do you have an e-mail where i can send it?

Thanks
Raj</description>
		<content:encoded><![CDATA[<p>Hi,<br />
Query is not fitting in the comments box, do you have an e-mail where i can send it?</p>
<p>Thanks<br />
Raj</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Raj</title>
		<link>http://www.ardentperf.com/2007/05/18/combining-hierarchical-queries-with-analytics/comment-page-1/#comment-169</link>
		<dc:creator>Raj</dc:creator>
		<pubDate>Fri, 18 May 2007 15:51:53 +0000</pubDate>
		<guid isPermaLink="false">http://www.ardentperf.com/2007/05/18/combining-hierarchical-queries-with-analytics/#comment-169</guid>
		<description>SELECT rm,
       lvl,
       ename,
       empno,
       sal_str,
       first_value(no_space_ename) OVER (PARTITION BY vp_num) vp_name,
       sal,
       (
          SELECT sum( to_number (SUBSTR (sal_str, INSTR(sal_str , &#039;+&#039;, 1, ROWNUM ) + 1, INSTR(sal_str, &#039;+&#039;, 1, ROWNUM + 1) - INSTR(sal_str , &#039;+&#039;, 1, ROWNUM ) - 1)))
          FROM dual
          CONNECT BY LEVEL </description>
		<content:encoded><![CDATA[<p>SELECT rm,<br />
       lvl,<br />
       ename,<br />
       empno,<br />
       sal_str,<br />
       first_value(no_space_ename) OVER (PARTITION BY vp_num) vp_name,<br />
       sal,<br />
       (<br />
          SELECT sum( to_number (SUBSTR (sal_str, INSTR(sal_str , &#8216;+&#8217;, 1, ROWNUM ) + 1, INSTR(sal_str, &#8216;+&#8217;, 1, ROWNUM + 1) &#8211; INSTR(sal_str , &#8216;+&#8217;, 1, ROWNUM ) &#8211; 1)))<br />
          FROM dual<br />
          CONNECT BY LEVEL</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Raj</title>
		<link>http://www.ardentperf.com/2007/05/18/combining-hierarchical-queries-with-analytics/comment-page-1/#comment-167</link>
		<dc:creator>Raj</dc:creator>
		<pubDate>Fri, 18 May 2007 15:49:44 +0000</pubDate>
		<guid isPermaLink="false">http://www.ardentperf.com/2007/05/18/combining-hierarchical-queries-with-analytics/#comment-167</guid>
		<description>Hi,

I used a query for parsing the sal string.

SELECT rm,
       lvl,
       ename,
       empno,
       sal_str,
       first_value(no_space_ename) OVER (PARTITION BY vp_num) vp_name,
       sal,
       (
          SELECT sum( 
                      to_number (
                        SUBSTR (sal_str, 
                          INSTR(sal_str , &#039;+&#039;, 1, ROWNUM ) + 1, 
                          INSTR(sal_str, &#039;+&#039;, 1, ROWNUM + 1) - 
                            INSTR(sal_str , &#039;+&#039;, 1, ROWNUM ) - 1
                            ))
                    )
          FROM dual
          CONNECT BY LEVEL </description>
		<content:encoded><![CDATA[<p>Hi,</p>
<p>I used a query for parsing the sal string.</p>
<p>SELECT rm,<br />
       lvl,<br />
       ename,<br />
       empno,<br />
       sal_str,<br />
       first_value(no_space_ename) OVER (PARTITION BY vp_num) vp_name,<br />
       sal,<br />
       (<br />
          SELECT sum(<br />
                      to_number (<br />
                        SUBSTR (sal_str,<br />
                          INSTR(sal_str , &#8216;+&#8217;, 1, ROWNUM ) + 1,<br />
                          INSTR(sal_str, &#8216;+&#8217;, 1, ROWNUM + 1) &#8211;<br />
                            INSTR(sal_str , &#8216;+&#8217;, 1, ROWNUM ) &#8211; 1<br />
                            ))<br />
                    )<br />
          FROM dual<br />
          CONNECT BY LEVEL</p>
]]></content:encoded>
	</item>
</channel>
</rss>
