【发布时间】:2012-02-19 16:24:29
【问题描述】:
我正在使用 SQLite3,并希望按月获取数字字段的总计。模型看起来像:
# Table name: accounts
# id :integer not null, primary key
# created_at :datetime
# updated_at :datetime
class Account < ActiveRecord::Base
has_many :debitentries, :class_name => "Posting", :foreign_key => "debitaccount_id"
has_many :creditentries, :class_name => "Posting", :foreign_key => "creditaccount_id"
# Table name: postings
# id :integer not null, primary key
# voucherdate :date
# debitaccount_id :integer
# creditaccount_id :integer
# euroamount :decimal(, )
Class Postings < ActiveRecord::Base
belongs_to :debitaccount, :class_name => "Account", :foreign_key => "debitaccount_id"
belongs_to :creditaccount, :class_name => "Account", :foreign_key => "creditaccount_id"
我的目标是查询凭证日期
Account.id| Feb 2012 | Jan 2012 | Dec 2011 | ... | Mar 2011
------------------------------------------------------------
1 | 233.87 | 123.72 | ... | | sum(euroamount)
2 | ... | | | |
我想我需要两个查询(一个用于借方的总和,一个用于贷方的总和),但我认为它比使用 rails-functions 更有效。谁能帮我这样的查询应该是什么样子? 非常感谢!
【问题讨论】:
标签: sql ruby-on-rails ruby-on-rails-3 sqlite