~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to docs/groupby.rst

  • Committer: Andrew Hutchings
  • Date: 2011-02-15 22:03:42 UTC
  • mto: (2172.1.2 build)
  • mto: This revision was merged to the branch mainline in revision 2173.
  • Revision ID: andrew@linuxjedi.co.uk-20110215220342-k4tveraug3bpy2v5
Make fixes to temporal docs
Make a few minor docs cleanups/fixes
Start using syntax-highlighting code blocks

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