4
The GROUP BY clause is used to extract only those records that fulfill a specified criterion.
8
SELECT column_name, aggregate_function(column_name)
10
WHERE column_name operator value
14
**GROUP BY Clause Example**
16
The "Activities" table:
18
+---------+--------------+--------------+-------------+----------+
19
|Id |ActivityDate |ActivityType |ActivityCost | userID |
20
+=========+==============+==============+=============+==========+
21
| 1 |2011-01-02 | Sport |45 |131 |
22
+---------+--------------+--------------+-------------+----------+
23
| 2 |2011-01-02 | Art |10 |256 |
24
+---------+--------------+--------------+-------------+----------+
25
| 3 |2011-01-02 | Music |25 |022 |
26
+---------+--------------+--------------+-------------+----------+
27
| 4 |2011-01-02 | Food |125 |022 |
28
+---------+--------------+--------------+-------------+----------+
29
| 5 |2011-01-03 | Music |40 |131 |
30
+---------+--------------+--------------+-------------+----------+
31
| 6 |2011-01-03 | Food |20 |175 |
4
The GROUP BY clause is used to extract only those records that fulfill a specified criterion.
8
SELECT column_name, aggregate_function(column_name)
10
WHERE column_name operator value
13
**GROUP BY Clause Example**
15
The "Activities" table:
17
+---------+--------------+--------------+-------------+----------+
18
|Id |ActivityDate |ActivityType |ActivityCost | userID |
19
+=========+==============+==============+=============+==========+
20
| 1 |2011-01-02 | Sport |45 |131 |
21
+---------+--------------+--------------+-------------+----------+
22
| 2 |2011-01-02 | Art |10 |256 |
23
+---------+--------------+--------------+-------------+----------+
24
| 3 |2011-01-02 | Music |25 |022 |
25
+---------+--------------+--------------+-------------+----------+
26
| 4 |2011-01-02 | Food |125 |022 |
27
+---------+--------------+--------------+-------------+----------+
28
| 5 |2011-01-03 | Music |40 |131 |
29
+---------+--------------+--------------+-------------+----------+
30
| 6 |2011-01-03 | Food |20 |175 |
32
31
+---------+--------------+--------------+-------------+----------+
34
33
Running the following simple query::
54
(This shows that GROUP BY accepts a column_name and consolidates like customer values.)
56
However, GROUP BY is much more powerful when used with an aggregate function. Let's say you want to find the total amount spent by each unique User.
58
You could use the following SQL statement: ::
53
(This shows that GROUP BY accepts a column_name and consolidates like customer values.)
55
However, GROUP BY is much more powerful when used with an aggregate function. Let's say you want to find the total amount spent by each unique User.
57
You could use the following SQL statement: ::
60
59
SELECT userID,SUM(ActivityCost) AS "Activity Total"
64
63
The result-set will look like this:
75
With the aggregate SUM() function, SQL can calculate how much each unique user has spent on activities over time.
77
We can also use the GROUP BY statement on more than one column, like this: ::
79
SELECT userID,ActivityDate,SUM(ActivityCost)
74
With the aggregate SUM() function, SQL can calculate how much each unique user has spent on activities over time.
76
We can also use the GROUP BY statement on more than one column, like this: ::
78
SELECT userID,ActivityDate,SUM(ActivityCost)
81
80
GROUP BY userID,ActivityDate;