24
19

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【Rails】ActiveRecordで、月ごと年ごとの合計

Posted at

Rails、ActiveRecordでの書き方。
月別の合計を出す機会があったので、色々調べてみて、
一番簡潔そうなのがこれでした。

sales_controller.rb
@sales = Sale.group("MONTH(date)").sum(:column)

groupでまとめて、sumしてます。
ちなみに、MONTHをYEARに変えれば年ごとになります。

もっと良いのがあれば教えてください。

24
19
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
24
19

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?