Oracle FAQ | Your Portal to the Oracle Knowledge Grid |
![]() |
![]() |
Home -> Community -> Usenet -> c.d.o.misc -> Re: SQL: discussion of GROUP BY clause
On 12 Nov 2001, nzanella_at_cs.mun.ca (Neil Zanella) spake and
said:
> Nicholas Carey <ncarey_at_speakeasy.org> wrote in message > news:<Xns9156F26955274ncareyspeakeasyorg_at_207.126.101.92>...
> > All I am saying is that the standard should be more > flexible. In the above example A is not the primary key of > the given table thus for each valuein > column A there can be multiple values for the corresponding > element in column B as you pointed out. However if A was a > primary key then I see no reason why the standard should not > accept the above query.
GROUP BY cares not a whit about keys, primary or other. All GROUP BY does is collapse the set so that one row is returned for every distinct combination of values for the columns specified in the GROUP BY list. Another way of looking at it is to view it as sequencing the set by the group by list, then processing it in sequence. At each sequence break, one row and one row only is emitted. As it processes rows, group by can tote up the various aggregate functions -- count(), sum(), avg(), etc.
But that's it. That's group by's functionality, in toto.
And in that context, the notion of returning something *other* than a constant or an aggregate makes no sense whatsoever.-- it's undefined. Taking my previous example -- if you issue this select statement
select A, B from foo group by A
against a table with two columsn, A and B, consider a group of 1000 rows: each row has a common value for A, and each row has a unique, distinct value for B. What on earth could this select statement possibly return that would make sense in the context of what GROUP BY is designed to do?
Micro$oft (and Sybase's) SQL Server used to allow [emphasis on the 'used to' part -- v4.x and earlier] allow non-constant, nonaggregating and non-grouping stuff in a select statements containing group by. Something along the lines of what you are talking about:
select A, B, count(*) from foo group by A
Which didn't do grouping, but instead performed an [undocumented] equivalent of a join (SQL-92 syntax):
select t1.A , t1.B , t2.
from foo group by A ) t2 on t2.A = t2.A
And if that's what you want, why not just ask for it? This behaviour was Bad because the semantics of GROUP BY changed (morphed) depending on what you put in the result list and in the group by list. A minor error in an expression and you got something completely unrelated to what you were looking for. Received on Mon Nov 12 2001 - 23:59:59 CST
![]() |
![]() |