質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.47%
Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Active Record

Active Recordは、一つのオブジェクトに対しドメインのロジックとストレージの抽象性を結合するデザインパターンです。

Q&A

0回答

531閲覧

railsでactive recordのwhereで条件をつけて集計結果が変わるようにしたい。

ganbarou_nippon

総合スコア18

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Active Record

Active Recordは、一つのオブジェクトに対しドメインのロジックとストレージの抽象性を結合するデザインパターンです。

0グッド

0クリップ

投稿2020/02/20 07:19

編集2020/02/20 08:26

ruby

1def index 2 @user=current_user 3 @notes=@user.notes.all.order("id DESC").page(params[:page]).per(30) 4 notes_all=@user.notes 5 if notes_all.present? 6 notes_all_plus = notes_all.where('profit_and_loss > ?', 0 ) 7 notes_all_index=notes_all.group("YEAR(exit_datetime)").group("MONTH(exit_datetime)") 8 notes_all_plus_index= notes_all_plus.group("YEAR(exit_datetime)").group("MONTH(exit_datetime)") 9 notes_all_times=notes_all.group("HOUR(CONVERT_TZ(exit_datetime, '+00:00', '+09:00'))") 10 notes_all_plus_times= notes_all_plus.group("HOUR(CONVERT_TZ(exit_datetime, '+00:00', '+09:00'))") 11 @profit_symbol=notes_all.group(:symbol).sum(:profit_and_loss) 12 @profit_changes = notes_all_index.sum(:profit_and_loss).values.map.with_index{|money,index| [notes_all_index.sum(:profit_and_loss).keys[index],money.to_f/notes_all.sum(:profit_and_loss)*100]} 13 @profit_times=notes_all_times.sum(:profit_and_loss).values.map.with_index{|money, index| [notes_all_times.sum(:profit_and_loss).keys[index],money.to_f/notes_all.sum(:profit_and_loss)*100]} 14 @victry_times=notes_all_plus_times.count.values.map.with_index{|money,index| [notes_all_times.count.keys[index], money.to_f/notes_all_times.count.values[index]*100]} 15 @victory_rates=notes_all_plus_index.count.values.map.with_index{|money,index| [notes_all_index.count.keys[index],money.to_f/notes_all_index.count.values[index]*100]} 16 17 if notes_all.where(buy_sell: 1).present? 18 notes_all_short=notes_all.where(buy_sell: 1).group("YEAR(exit_datetime)").group("MONTH(exit_datetime)") 19 notes_all_plus_short=notes_all_plus.where(buy_sell: 1).group("YEAR(exit_datetime)").group("MONTH(exit_datetime)") 20 @profit_short=notes_all_short.sum(:profit_and_loss).values.map.with_index{|money,index| [notes_all_index.sum(:profit_and_loss).keys[index],money.to_f/notes_all_index.sum(:profit_and_loss).values[index]*100]} 21 @victry_short=notes_all_plus_short.count.values.map.with_index{|money,index| [notes_all_short.count.keys[index],money.to_f/notes_all_short.count.values[index]*100]} 22 end 23 24 if notes_all.where(buy_sell: 0).present? 25 notes_all_long=notes_all.where(buy_sell: 0).group("YEAR(exit_datetime)").group("MONTH(exit_datetime)") 26 notes_all_plus_long=notes_all_plus.where(buy_sell: 0).group("YEAR(exit_datetime)").group("MONTH(exit_datetime)") 27 @profit_long=notes_all_long.sum(:profit_and_loss).values.map.with_index{|money,index| [notes_all_index.sum(:profit_and_loss).keys[index],money.to_f/notes_all_index.sum(:profit_and_loss).values[index]*100]} 28 @victry_long=notes_all_plus_long.count.values.map.with_index{|money,index| [notes_all_index.count.keys[index],money.to_f/notes_all_long.count.values[index]*100]} 29 end 30 this_month = Date.today.all_month 31 before_month=Date.today.last_month 32 profit_this_month = [] 33 profit_before_month = [] 34 notes_all.each do |note| 35 if this_month.include?(Date.parse(note[:created_at].to_s)) 36 profit_this_month << note.profit_and_loss 37 elsif before_month.include?(Date.parse(note[:created_at].to_s)) 38 profit_before_month << note.profit_and_loss 39 end 40 end 41 @profit_this_month= profit_this_month.sum 42 @profit_this_month_average = @profit_this_month / profit_this_month.count 43 @victory_rate_this_month = profit_this_month.count{|money| money > 0}/profit_this_month.count.to_f*100 44 if profit_before_month.present? 45 @profit_before_month= profit_before_month.sum 46 @profit_before_month_average= @profit_before_month / profit_before_month.count 47 @victory_rate_before_month=profit_before_month.count{|money| money > 0}/profit_before_month.count.to_f*100 48 end 49 if notes_all_plus.present? && notes_all.where('profit_and_loss < ?', 0).present? 50 notes_all_minus_index=notes_all.where('profit_and_loss < ?', 0).group("YEAR(exit_datetime)").group("MONTH(exit_datetime)") 51 @profit_factor= notes_all_plus_index.sum(:profit_and_loss).values.map.with_index{|money,index| [ notes_all_plus_index.sum(:profit_and_loss).keys[index],money/ notes_all_minus_index.sum(:profit_and_loss).values[index].to_f.abs]} 52 @risk_reward_factor= notes_all_plus_index.average(:profit_and_loss).values.map.with_index{|money,index| [ notes_all_plus_index.average(:profit_and_loss).keys[index],money/ notes_all_minus_index.average(:profit_and_loss).values[index].to_f.abs]} 53 @recovery_factor=notes_all_index.sum(:profit_and_loss).values.map.with_index{|money,index| [notes_all_index.sum(:profit_and_loss).keys[index],money/notes_all_minus_index.sum(:profit_and_loss).values[index].to_f.abs]} 54 end 55 respond_to do |format| 56 format.html 57 format.csv do |csv| 58 send_notes_csv(notes_all) 59 end 60 end 61 end 62 end

このようなコードで、@victory_rates=notes_all_plus_index.count.values.map.with_index{|money,index| [notes_all_index.count.keys[index],money.to_f/notes_all_index.count.values[index]*100]}部分についてです。

money.to_fはwhere('profit_and_loss > ?', 0 )という条件がついてnotes_all_index.count.values[index]と数が本来違うはずなのですが、計算結果が100になり、同数になってしまいます。

他にも以下のような部分で同様の問題で困っています。

@victry_short=notes_all_plus_short.count.values.map.with_index{|money,index| [notes_all_short.count.keys[index],money.to_f/notes_all_short.count.values[index]*100]}

@victry_long=notes_all_plus_long.count.values.map.with_index{|money,index| [notes_all_index.count.keys[index],money.to_f/notes_all_long.count.values[index]*100

原因として ```ruby .where('profit_and_loss > ?', 0 )

の部分が効いていないのかとも考えました。

これが実際のSQL文です。

イメージ説明

イメージ説明

イメージ説明

イメージ説明

原因がわからず、困惑しています。

どうすれば、正しい計算結果が得られるでしょうか?

イメージ説明

今回の場合データが以上のようになっていて、正しい回答は50のはずなのですが…

解決策が分かる方ご教示いただけると幸いです。

何卒よろしくお願い申し上げます。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

Mugheart

2020/02/20 08:41 編集

申し訳ないですが、まともに読めたものじゃないコードです。 このコードを綺麗に書き直せとは言えないので、そのままのコードではなく、 何か再現のできるような少量のサンプルを別に用意していただけますか? それが難しいようでしたら、もう少し読みやすいように整形をお願いします。 最低限インデントは正しくつけてください。 横に長すぎるコードも適切に改行していただけると多少は読みやすくなります。 コード量も要点を絞って減らせてもらえるとなお良いです。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.47%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問