# Complex query

**URL:** <https://www.ruby-forum.com/t/complex-query/165767>\
**Category:** Rails\
**Created:** [April 17, 2009, 9:45am UTC](https://www.ruby-forum.com/t/complex-query/165767 "2009-04-17T09:45:48Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![random-query](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/bc79bd/32.png) [@random-query](https://www.ruby-forum.com/u/random-query)\
**Post date:** [April 17, 2009, 9:45am UTC](https://www.ruby-forum.com/t/complex-query/165767/1 "2009-04-17T09:45:48Z")

</div>

Hi,  
Part of my Invoice table looks like this:

create\_table “invoices”, :force =\> true do |t|  
t.datetime “sent\_at”,  
t.datetime “received\_payment\_at”,  
t.integer “total\_fee”  
…  
end

I want to put together view with monthly sums for ‘invoiced amounts’ and  
‘received amounts’ like this:

month | total invoiced | total received  
Feb 08 | 20,000 | 15,000  
Mar 08 | 35,000 | 30,000

What’ the best way to write a query & a view to achieve this?

Thanks!  
RQ
