Skip to content

brownbeagle/cross_tabulate

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

Perform efficient cross tabulations of data through ActiveRecord

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages