-
Notifications
You must be signed in to change notification settings - Fork 0
/
README
47 lines (35 loc) · 1.33 KB
/
README
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
CrossTabulate
=============
Perform efficient cross tabulations in ActiveRecord.
A cross tabulation compares one variable against the tabulation of another variable. This is often useful when you need to aggregate data in categories.
For example, you may want to find the sum of sales for each department in a supermarket. Given the schema:
Departments: id, name
Sales: id, department_id, amount
and the example data:
Departments:
+------------------+
| id | name |
|----+-------------+
| 1 | Vegetables |
| 2 | Deli |
| 3 | Frozen |
+------------------+
Sales:
+-----------------------------+
| id | department_id | amount |
|----+---------------+--------+
| 1 | 1 | 11 |
| 1 | 1 | 111 |
| 2 | 2 | 22 |
| 2 | 2 | 222 |
| 3 | 3 | 3 |
+-----------------------------+
You would want the output:
+----------------------------+
| Vegetables | Deli | Frozen |
|------------+------+--------+
| 122 | 244 | 3 |
+----------------------------+
Which could be achieved using the method call:
Department.cross('name', :aggregator => 'sum', aggregator_column => 'amount', :joins => 'INNER JOIN sales ON sales.department_id = departments.id')
Copyright (c) 2009 Brown Beagle Software, released under the MIT license