~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to docs/groupby.rst

  • Committer: Lee Bieber
  • Date: 2011-01-10 20:12:43 UTC
  • mfrom: (2069.1.3 build)
  • Revision ID: kalebral@gmail.com-20110110201243-j7xse6v4ptaipc92
Merge Gustaf - Use constrained_check<> classes on kernel sysvars 
Merge Andrew - fix bug 645004: libuuid configure error message needs changing
Merge Stewart - fix bug 698315: innobase tests fail with --repeat=2

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
Group By
2
 
========
3
 
 
4
 
The GROUP BY clause is used to extract only those records that fulfill a specified criterion.
5
 
 
6
 
SQL GROUP BY Syntax
7
 
 
8
 
.. code-block:: mysql
9
 
 
10
 
        SELECT column_name, aggregate_function(column_name)
11
 
        FROM table_name
12
 
        WHERE column_name operator value
13
 
        GROUP BY column_name
14
 
 
15
 
**GROUP BY Clause Example**
16
 
 
17
 
The "Activities" table:
18
 
 
19
 
+---------+--------------+--------------+-------------+----------+
20
 
|Id       |ActivityDate  |ActivityType  |ActivityCost | userID   |
21
 
+=========+==============+==============+=============+==========+
22
 
| 1       |2011-01-02    | Sport        |45           |131       |
23
 
+---------+--------------+--------------+-------------+----------+
24
 
| 2       |2011-01-02    | Art          |10           |256       |
25
 
+---------+--------------+--------------+-------------+----------+
26
 
| 3       |2011-01-02    | Music        |25           |022       |
27
 
+---------+--------------+--------------+-------------+----------+
28
 
| 4       |2011-01-02    | Food         |125          |022       |
29
 
+---------+--------------+--------------+-------------+----------+
30
 
| 5       |2011-01-03    | Music        |40           |131       |
31
 
+---------+--------------+--------------+-------------+----------+
32
 
| 6       |2011-01-03    | Food         |20           |175       |
33
 
+---------+--------------+--------------+-------------+----------+
34
 
 
35
 
Running the following simple query
36
 
 
37
 
.. code-block:: mysql
 
1
Group By
 
2
========
 
3
 
 
4
The GROUP BY clause is used to extract only those records that fulfill a specified criterion.
 
5
 
 
6
SQL GROUP BY Syntax ::
 
7
 
 
8
        SELECT column_name, aggregate_function(column_name)
 
9
        FROM table_name
 
10
        WHERE column_name operator value
 
11
        GROUP BY column_name
 
12
 
 
13
        
 
14
**GROUP BY Clause Example**
 
15
 
 
16
The "Activities" table:
 
17
 
 
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       |
 
32
+---------+--------------+--------------+-------------+----------+
 
33
 
 
34
Running the following simple query::
38
35
 
39
36
        SELECT userID
40
37
        FROM activities
41
 
        GROUP BY userID;
42
 
 
 
38
        GROUP BY userID;
 
39
 
43
40
Returns:
44
41
 
45
 
+---------+
46
 
| userID  |
47
 
+=========+
48
 
| 131     |
49
 
+---------+
50
 
| 256     |
51
 
+---------+
52
 
| 022     |
53
 
+---------+
54
 
| 175     |
55
 
+---------+
56
 
 
57
 
(This shows that GROUP BY accepts a column_name and consolidates like customer values.)
58
 
 
59
 
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.
60
 
 
61
 
You could use the following SQL statement:
62
 
 
63
 
.. code-block:: mysql
64
 
 
 
42
+---------+
 
43
| userID  |
 
44
+=========+
 
45
| 131     |
 
46
+---------+
 
47
| 256     |
 
48
+---------+
 
49
| 022     |
 
50
+---------+
 
51
| 175     |
 
52
+---------+
 
53
 
 
54
(This shows that GROUP BY accepts a column_name and consolidates like customer values.)
 
55
 
 
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.
 
57
 
 
58
You could use the following SQL statement: ::
 
59
        
65
60
        SELECT userID,SUM(ActivityCost) AS "Activity Total"
66
61
        FROM Activities
67
 
        GROUP BY userID;
68
 
 
 
62
        GROUP BY userID;
 
63
 
69
64
The result-set will look like this:
70
 
 
71
 
======    ==============
72
 
userID    Activity Total
73
 
======    ==============
74
 
131       85
75
 
256       10
76
 
022       150
77
 
175       20
78
 
======    ==============
79
 
 
80
 
With the aggregate SUM() function, SQL can calculate how much each unique user has spent on activities over time.
81
 
 
82
 
We can also use the GROUP BY statement on more than one column, like this:
83
 
 
84
 
.. code-block:: mysql
85
 
 
86
 
        SELECT userID,ActivityDate,SUM(ActivityCost)
87
 
        FROM Activities
 
65
 
 
66
======    ==============
 
67
userID    Activity Total
 
68
======    ==============
 
69
131       85             
 
70
256       10             
 
71
022       150            
 
72
175       20             
 
73
======    ==============
 
74
 
 
75
With the aggregate SUM() function, SQL can calculate how much each unique user has spent on activities over time.
 
76
 
 
77
We can also use the GROUP BY statement on more than one column, like this: ::
 
78
        
 
79
        SELECT userID,ActivityDate,SUM(ActivityCost) 
 
80
        FROM Activities
88
81
        GROUP BY userID,ActivityDate;
89
82