diff --git a/lib/ruport/data/table.rb b/lib/ruport/data/table.rb index 8a6654e2..382e75cd 100644 --- a/lib/ruport/data/table.rb +++ b/lib/ruport/data/table.rb @@ -79,7 +79,7 @@ def to_table outer_group = outer_grouping[outer_group_name] pivot_values = pivoted_columns.inject({}) do |hsh, e| matching_rows = outer_group.rows_with(@pivot_column => e) - hsh[e] = matching_rows.first && matching_rows.first[@summary_column] + hsh[e] = matching_rows && matching_rows.inject(0) { |sum,row| sum + row[@summary_column] } hsh end result << [outer_group_name] + pivoted_columns.map {|e| diff --git a/test/table_pivot_test.rb b/test/table_pivot_test.rb index a2f0ff32..5a2b78e1 100644 --- a/test/table_pivot_test.rb +++ b/test/table_pivot_test.rb @@ -132,3 +132,20 @@ def test_preserves_ordering_on_calculated_column_with_proc_pivot_order end end + +class TablePivotaggregationTest < Test::Unit::TestCase + + def setup + table = Table('Region', 'Product', 'Units Sold') + table << ['North','Widget',5] + table << ['North','Widget',10] + table << ['South','Gadget',2] + table << ['South','Gadget',4] + @pivoted = table.pivot('Product', :group_by => 'Region', :values => 'Units Sold') + end + + def test_produces_correct_full_table_with_sum + expected = Table("Region","Gadget","Widget") { |t| t << ["North",0,15] << ["South",6,0] } + assert_equal(expected, @pivoted) + end +end \ No newline at end of file