~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to docs/groupby.rst

edit

Show diffs side-by-side

added added

removed removed

Lines of Context:
3
3
 
4
4
The GROUP BY clause is used to extract only those records that fulfill a specified criterion.
5
5
 
6
 
SQL GROUP BY Syntax
7
 
 
8
 
.. code-block:: mysql
 
6
SQL GROUP BY Syntax ::
9
7
 
10
8
        SELECT column_name, aggregate_function(column_name)
11
9
        FROM table_name
32
30
| 6       |2011-01-03    | Food         |20           |175       |
33
31
+---------+--------------+--------------+-------------+----------+
34
32
 
35
 
Running the following simple query
36
 
 
37
 
.. code-block:: mysql
 
33
Running the following simple query::
38
34
 
39
35
        SELECT userID
40
36
        FROM activities
58
54
 
59
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.
60
56
 
61
 
You could use the following SQL statement:
62
 
 
63
 
.. code-block:: mysql
 
57
You could use the following SQL statement: ::
64
58
 
65
59
        SELECT userID,SUM(ActivityCost) AS "Activity Total"
66
60
        FROM Activities
79
73
 
80
74
With the aggregate SUM() function, SQL can calculate how much each unique user has spent on activities over time.
81
75
 
82
 
We can also use the GROUP BY statement on more than one column, like this:
83
 
 
84
 
.. code-block:: mysql
 
76
We can also use the GROUP BY statement on more than one column, like this: ::
85
77
 
86
78
        SELECT userID,ActivityDate,SUM(ActivityCost)
87
79
        FROM Activities