Re: GROUP BY...easier way to think about it
Posted in 1998
Ian Clark wrote:
>
> Hi All,
>
> For goodness sake people, I'm still trying to get my aching brain
> around this thing of GROUP BY :-)
>
> One way I have tried to think about it is to view a GROUP BY statement
> on 1 or more rows as though it were selecting a distinct row. That is:
>
> SELECT field1, field2, field3
> FROM table
> WHERE conditions
> GROUP BY 1, 2, 3>
> Where, in effect, the rows returned will be unique and that the
> combination of the 3 fields is acting as a key.
>
> Er, does anyone follow me or am I barking mad.
Wellll... In your specific example the GROUP BY has indeed trivially
become a DISTINCT but probably a little more expensive. Generally
GROUP BY is used to sumarize records on some set of columns and return
and AGGREGATE value (like COUNT(), SUM(), AVG(), MAX(), MIN()) for
each unique set of the key columns. Ex:
SELECT s.DeptNo, DeptName, SUM(Salary)
FROM salary s dept d
WHERE s.deptno = d.deptno
GROUP BY 1, 2;
This returns the total salary budget by department.
Art S. Kagel