<?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"
	>
<channel>
	<title>Comments on: Why cannot I use subquery there?</title>
	<atom:link href="http://laurentschneider.com/wordpress/2008/01/why-cannot-i-use-subquery-there.html/feed/" rel="self" type="application/rss+xml" />
	<link>http://laurentschneider.com/wordpress/2008/01/why-cannot-i-use-subquery-there.html</link>
	<description>Oracle Certified Master</description>
	<pubDate>Mon, 01 Dec 2008 20:46:59 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6.5</generator>
		<item>
		<title>By: Laurent Schneider</title>
		<link>http://laurentschneider.com/wordpress/2008/01/why-cannot-i-use-subquery-there.html#comment-5864</link>
		<dc:creator>Laurent Schneider</dc:creator>
		<pubDate>Mon, 28 Jan 2008 14:50:52 +0000</pubDate>
		<guid isPermaLink="false">http://laurentschneider.com/wordpress/2008/01/why-cannot-i-use-subquery-there.html#comment-5864</guid>
		<description>Oracle fails in noticing the scalar subquery is constant and not correlated.

I filed a documentation bug since GROUP BY () is not documented in the SQL Reference but it is implictely documented Oracle® Database Data Warehousing Guide, SQL for Aggregation in Data Warehouses 
&lt;i&gt;GROUP BY ROLLUP(a, (b, c))
This is equivalent to:

GROUP BY a, b, c UNION ALL
GROUP BY a UNION ALL
GROUP BY ()
&lt;/i&gt;
note the latest group by !

in the following query, the group by clause is mandatory, because the subquery is correlated

 &lt;pre&gt;
select
    (select dname from dept where emp.deptno=dept.deptno),
  count(*)
  from emp
  group by deptno;

(SELECTDNAMEFR   COUNT(*)
-------------- ----------
SALES                   6
RESEARCH                5
ACCOUNTING              3
&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>Oracle fails in noticing the scalar subquery is constant and not correlated.</p>
<p>I filed a documentation bug since GROUP BY () is not documented in the SQL Reference but it is implictely documented Oracle® Database Data Warehousing Guide, SQL for Aggregation in Data Warehouses<br />
<i>GROUP BY ROLLUP(a, (b, c))<br />
This is equivalent to:</p>
<p>GROUP BY a, b, c UNION ALL<br />
GROUP BY a UNION ALL<br />
GROUP BY ()<br />
</i><br />
note the latest group by !</p>
<p>in the following query, the group by clause is mandatory, because the subquery is correlated</p>
<p> <pre><pre>
select
&nbsp;&nbsp;&nbsp;&nbsp;(select dname from dept where emp.deptno=dept.deptno),
&nbsp;&nbsp;count(*)
&nbsp;&nbsp;from emp
&nbsp;&nbsp;group by deptno;

(SELECTDNAMEFR&nbsp;&nbsp; COUNT(*)
-------------- ----------
SALES&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 6
RESEARCH&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;5
ACCOUNTING&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;3
</pre></pre></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Martin Smevik</title>
		<link>http://laurentschneider.com/wordpress/2008/01/why-cannot-i-use-subquery-there.html#comment-5862</link>
		<dc:creator>Martin Smevik</dc:creator>
		<pubDate>Mon, 28 Jan 2008 13:27:56 +0000</pubDate>
		<guid isPermaLink="false">http://laurentschneider.com/wordpress/2008/01/why-cannot-i-use-subquery-there.html#comment-5862</guid>
		<description>I would guess that the reason for the apparent difference is that you can't have an aggregate function/or group function in a non-group query. The function count() requires that only one row is returned, while the sub-query will return an unknown amount of rows.

This goes for this example also:
SELECT COUNT(*), TABLE_NAME
FROM USER_TABLES;

ERROR at line 1:
ORA-00937: not a single-group group function

I would like to state that this is merely a undocumented theory. Anyone have a better explanation?</description>
		<content:encoded><![CDATA[<p>I would guess that the reason for the apparent difference is that you can&#8217;t have an aggregate function/or group function in a non-group query. The function count() requires that only one row is returned, while the sub-query will return an unknown amount of rows.</p>
<p>This goes for this example also:<br />
SELECT COUNT(*), TABLE_NAME<br />
FROM USER_TABLES;</p>
<p>ERROR at line 1:<br />
ORA-00937: not a single-group group function</p>
<p>I would like to state that this is merely a undocumented theory. Anyone have a better explanation?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Laurent Schneider</title>
		<link>http://laurentschneider.com/wordpress/2008/01/why-cannot-i-use-subquery-there.html#comment-5842</link>
		<dc:creator>Laurent Schneider</dc:creator>
		<pubDate>Fri, 25 Jan 2008 05:53:18 +0000</pubDate>
		<guid isPermaLink="false">http://laurentschneider.com/wordpress/2008/01/why-cannot-i-use-subquery-there.html#comment-5842</guid>
		<description>me neither 8-)</description>
		<content:encoded><![CDATA[<p>me neither <img src='http://laurentschneider.com/wordpress/wp-includes/images/smilies/icon_cool.gif' alt='8-)' class='wp-smiley' /></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Chen Shapira</title>
		<link>http://laurentschneider.com/wordpress/2008/01/why-cannot-i-use-subquery-there.html#comment-5841</link>
		<dc:creator>Chen Shapira</dc:creator>
		<pubDate>Fri, 25 Jan 2008 04:13:45 +0000</pubDate>
		<guid isPermaLink="false">http://laurentschneider.com/wordpress/2008/01/why-cannot-i-use-subquery-there.html#comment-5841</guid>
		<description>Isn't this query:

SQL&#62; SELECT (SELECT COUNT(*) FROM EMP),
  COUNT(*) FROM DEPT ;

equivalent to this:

SQL&#62; SELECT (SELECT COUNT(*) FROM EMP),
  COUNT(*) FROM DEPT GROUP BY ();

?

Don't see the reason to fail one and allow the other.</description>
		<content:encoded><![CDATA[<p>Isn&#8217;t this query:</p>
<p>SQL&gt; SELECT (SELECT COUNT(*) FROM EMP),<br />
  COUNT(*) FROM DEPT ;</p>
<p>equivalent to this:</p>
<p>SQL&gt; SELECT (SELECT COUNT(*) FROM EMP),<br />
  COUNT(*) FROM DEPT GROUP BY ();</p>
<p>?</p>
<p>Don&#8217;t see the reason to fail one and allow the other.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
