Icon View Thread

The following is the text of the current message along with any replies.
Messages 1 to 4 of 4 total
Thread SELECT statement with HAVING clause with no GROUP clause
Wed, Nov 17 2010 10:49 PMPermanent Link

Richard Harding

Wise Nutrition Coaching

Tim,

I tried executing a SELECT statement that has a HAVING clause but no GROUP BY clause.  I received an error that the GROUP BY clause is missing.  

My understanding is that you should be able to have a HAVING clause without the GROUP BY clause - the whole table is treated as 1 group.

Richard Harding
Thu, Nov 18 2010 8:31 AMPermanent Link

Tim Young [Elevate Software]

Elevate Software, Inc.

Avatar

Email timyoung@elevatesoft.com

Richard,

<< I tried executing a SELECT statement that has a HAVING clause but no
GROUP BY clause.  I received an error that the GROUP BY clause is missing.

My understanding is that you should be able to have a HAVING clause without
the GROUP BY clause - the whole table is treated as 1 group. >>

I'll double-check and see what the standard says.  A HAVING on a single-row
SELECT query with aggregates really isn't particularly useful, and the
workaround obviously is to just conditionally handle the condition in code,
in terms of what you do with the single result set row.

--
Tim Young
Elevate Software
www.elevatesoft.com
Thu, Nov 18 2010 8:44 PMPermanent Link

Richard Harding

Wise Nutrition Coaching

Tim,

<<I'll double-check and see what the standard says.  A HAVING on a single-row SELECT query with aggregates really isn't particularly useful, and the workaround obviously is to just conditionally handle the condition in code,
in terms of what you do with the single result set row.>>

Most of the time it does not make much sense but I wanted to use it in an EXISTS clause which did make sense to me at the time.  Something like this:

SELECT AVG(x)  FROM Table1 AS T1 WHERE EXISTS
 (SELECT COUNT(*) FROM Table1 AS T2 WHERE <some condition> HAVING <conditions to do with COUNT>)


Richard Harding . . .
Fri, Nov 19 2010 1:47 PMPermanent Link

Tim Young [Elevate Software]

Elevate Software, Inc.

Avatar

Email timyoung@elevatesoft.com

Richard,

<< Most of the time it does not make much sense but I wanted to use it in an
EXISTS clause which did make sense to me at the time.  Something like this:
>>

Ahh, got it.

--
Tim Young
Elevate Software
www.elevatesoft.com
Image