<?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: Scalar functions in GROUP BY</title>
	<atom:link href="http://itknowledgeexchange.techtarget.com/itanswers/scalar-functions-in-group-by/feed/" rel="self" type="application/rss+xml" />
	<link>http://itknowledgeexchange.techtarget.com/itanswers/scalar-functions-in-group-by/</link>
	<description></description>
	<pubDate>Sat, 26 May 2012 09:35:02 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6.2</generator>
		<item>
		<title>By: TomLiotta</title>
		<link>http://itknowledgeexchange.techtarget.com/itanswers/scalar-functions-in-group-by/#comment-69283</link>
		<dc:creator>TomLiotta</dc:creator>
		<pubDate>Wed, 21 Oct 2009 01:33:21 +0000</pubDate>
		<guid isPermaLink="false">#comment-69283</guid>
		<description>And as a test, this works in V5R3:
[CODE]
SELECT
   count(*),days(SYSLCHG)
 FROM mytesttbl
   GROUP BY days(SYSLCHG)
   HAVING count(*)&#60;&#62;0
[/CODE]
As does this:
[CODE]
SELECT
   count(*),days(SYSLCHG)
 FROM mytesttbl
   GROUP BY days(SYSLCHG)
   HAVING count(*)&#60;&#62;0
   ORDER BY  1  desc
[/CODE]
The first discussion point by ARWinner is on the mark.

Of course, that assumes that TMSTP_COL is a timestamp.

Tom</description>
		<content:encoded><![CDATA[<p>And as a test, this works in V5R3:</p>
<pre>
SELECT
   count(*),days(SYSLCHG)
 FROM mytesttbl
   GROUP BY days(SYSLCHG)
   HAVING count(*)&lt;&gt;0
</pre>
<p>As does this:</p>
<pre>
SELECT
   count(*),days(SYSLCHG)
 FROM mytesttbl
   GROUP BY days(SYSLCHG)
   HAVING count(*)&lt;&gt;0
   ORDER BY  1  desc
</pre>
<p>The first discussion point by ARWinner is on the mark.</p>
<p>Of course, that assumes that TMSTP_COL is a timestamp.</p>
<p>Tom</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: TomLiotta</title>
		<link>http://itknowledgeexchange.techtarget.com/itanswers/scalar-functions-in-group-by/#comment-69231</link>
		<dc:creator>TomLiotta</dc:creator>
		<pubDate>Mon, 19 Oct 2009 20:58:39 +0000</pubDate>
		<guid isPermaLink="false">#comment-69231</guid>
		<description>I've always given my derived columns names because I've never worked out the rules. The SQL Reference (V5R3) has this to say:

If GROUP BY or HAVING is used:
[ULIST]
	[ELEMENT]Each column-name in the select list must identify a grouping expression or be specified within a column function:
[ULIST]
	[ELEMENT]– If the grouping expression is a column name, the select list may apply additional operators to the column name. For example, if the grouping expression is a column C1, the select list may contain C1+1.[/ELEMENT]
	[ELEMENT]– If the grouping expression is not a column name, the select list may not apply additional operators to the expression. For example, if the grouping expression is C1+1, the select list may contain C1+1, but not (C1+1)/8.[/ELEMENT]
[/ULIST]
[/ELEMENT]
[/ULIST]
That last bit -- "If the grouping expression is not a column name" -- sure makes it sound like a column name is not required. And the example -- "the grouping expression is C1+1" -- sure seems to show that a column name isn't required.

But nothing is said about a SCALAR function either way.

I've never figured out the exact rules, so I just do like others have suggested -- [DAYS(TMSTP_COL) as NewCol] -- in the SELECT list and reference [NewCol] elsewhere.

Tom</description>
		<content:encoded><![CDATA[<p>I&#8217;ve always given my derived columns names because I&#8217;ve never worked out the rules. The SQL Reference (V5R3) has this to say:</p>
<p>If GROUP BY or HAVING is used:</p>
<ul>
<li>Each column-name in the select list must identify a grouping expression or be specified within a column function:
<ul>
<li>– If the grouping expression is a column name, the select list may apply additional operators to the column name. For example, if the grouping expression is a column C1, the select list may contain C1+1.</li>
<li>– If the grouping expression is not a column name, the select list may not apply additional operators to the expression. For example, if the grouping expression is C1+1, the select list may contain C1+1, but not (C1+1)/8.</li>
</ul>
</li>
</ul>
<p>That last bit &#8212; &#8220;If the grouping expression is not a column name&#8221; &#8212; sure makes it sound like a column name is not required. And the example &#8212; &#8220;the grouping expression is C1+1&#8243; &#8212; sure seems to show that a column name isn&#8217;t required.</p>
<p>But nothing is said about a SCALAR function either way.</p>
<p>I&#8217;ve never figured out the exact rules, so I just do like others have suggested &#8212; [DAYS(TMSTP_COL) as NewCol] &#8212; in the SELECT list and reference [NewCol] elsewhere.</p>
<p>Tom</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Stanton</title>
		<link>http://itknowledgeexchange.techtarget.com/itanswers/scalar-functions-in-group-by/#comment-42112</link>
		<dc:creator>Stanton</dc:creator>
		<pubDate>Mon, 03 Jan 2005 10:48:37 +0000</pubDate>
		<guid isPermaLink="false">#comment-42112</guid>
		<description>Here is what you have to do, I've tested it to make sure it works.

select count(*) THECOUNT, X.M_DAYS
FROM (SELECT DAYS(TMSTP_COL) M_DAYS
      FROM MYTAB) X
GROUP BY X.M_DAYS
HAVING COUNT(*) &#62; 1
ORDER BY THECOUNT DESC

Stanton</description>
		<content:encoded><![CDATA[<p>Here is what you have to do, I&#8217;ve tested it to make sure it works.</p>
<p>select count(*) THECOUNT, X.M_DAYS<br />
FROM (SELECT DAYS(TMSTP_COL) M_DAYS<br />
      FROM MYTAB) X<br />
GROUP BY X.M_DAYS<br />
HAVING COUNT(*) &gt; 1<br />
ORDER BY THECOUNT DESC</p>
<p>Stanton</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: BigKat</title>
		<link>http://itknowledgeexchange.techtarget.com/itanswers/scalar-functions-in-group-by/#comment-42113</link>
		<dc:creator>BigKat</dc:creator>
		<pubDate>Sun, 02 Jan 2005 10:45:23 +0000</pubDate>
		<guid isPermaLink="false">#comment-42113</guid>
		<description>give the colums names and group by the column not the function.

select .... AS NAME .... group by NAME....</description>
		<content:encoded><![CDATA[<p>give the colums names and group by the column not the function.</p>
<p>select &#8230;. AS NAME &#8230;. group by NAME&#8230;.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: ARWinner</title>
		<link>http://itknowledgeexchange.techtarget.com/itanswers/scalar-functions-in-group-by/#comment-42114</link>
		<dc:creator>ARWinner</dc:creator>
		<pubDate>Fri, 31 Dec 2004 03:18:12 +0000</pubDate>
		<guid isPermaLink="false">#comment-42114</guid>
		<description>The ORDER BY clause is for sorting--after the result set is selected.  That is why the function is not allowed, it is sorta meaningless to count again.  Try this:

SELECT COUNT(*) as quantity, DAYS(TMSTP_COL)
FROM MYTAB A
GROUP BY DAYS(TMSTP_COL)
HAVING COUNT(*) &#62; 1
ORDER BY quantity DESC

or this:

SELECT COUNT(*), DAYS(TMSTP_COL)
FROM MYTAB A
GROUP BY DAYS(TMSTP_COL)
HAVING COUNT(*) &#62; 1
ORDER BY 1 DESC

HTH

Andy
</description>
		<content:encoded><![CDATA[<p>The ORDER BY clause is for sorting&#8211;after the result set is selected.  That is why the function is not allowed, it is sorta meaningless to count again.  Try this:</p>
<p>SELECT COUNT(*) as quantity, DAYS(TMSTP_COL)<br />
FROM MYTAB A<br />
GROUP BY DAYS(TMSTP_COL)<br />
HAVING COUNT(*) &gt; 1<br />
ORDER BY quantity DESC</p>
<p>or this:</p>
<p>SELECT COUNT(*), DAYS(TMSTP_COL)<br />
FROM MYTAB A<br />
GROUP BY DAYS(TMSTP_COL)<br />
HAVING COUNT(*) &gt; 1<br />
ORDER BY 1 DESC</p>
<p>HTH</p>
<p>Andy</p>
]]></content:encoded>
	</item>
</channel>
</rss>
<!-- dynamic -->
