2016-04-29 350 views
1

首先让我告诉你我想要什么,我想制作一个用户共同基金投资组合增长图表。如何将一年中的所有日常数据转换为月度数据

现在我有一个用户在去年投资一个投资组合。

现在我有一项任务,它将用户 投资组合的每日增长值存储在用户投资组合增长表中。

现在截至目前为止,我的用户投资组合增长表中的投资组合增长率均为日常价值。

我想要的是在图表上显示用户投资组合的月度增长。

因此,我的问题在于如何将所有每日价值转换为投资日期的月价值?所以,我可以在图表上显示它。

UserPortfolio架构

# == Schema Information 
# 
# Table name: user_portfolios 
# 
# id    :integer   not null, primary key 
# user_id   :integer   not null 
# portfolio_name :string 
# amount   :integer   not null 
# investment_plan :string   not null 
# duration  :integer   default(1), not null 
# risk_level  :string   not null 
# created_at  :datetime   not null 
# updated_at  :datetime   not null 

UserPortfolioGrowth

# == Schema Information 
# 
# Table name: user_portfolio_growths 
# 
# id    :integer   not null, primary key 
# user_portfolio_id :integer 
# appreciated_value :float 
# created_at  :datetime   not null 
# updated_at  :datetime   not null 
# 

谢谢!

编辑: 有人问我什么我已经做了的铁皮人: 所以,这是我迄今为止尝试了演示。

Inititally我尝试这种类型的代码:

start = 10.months.ago.to_date 
end_date = Date.today 
(start..end_date).each do |date| 
    puts date.end_of_month 
end 

为此我得到了巨大的结束日期的名单,因为它itterates在所有日期:

2015-07-31 
2015-07-31 
2015-07-31 
2015-07-31 
2015-07-31 
2015-07-31 
2015-07-31 
2015-07-31 
2015-07-31 
2015-07-31 
2015-07-31 
2015-07-31 
2015-07-31 
2015-07-31 
2015-07-31 
2015-07-31 
2015-07-31 
2015-07-31 
2015-07-31 
2015-07-31 
2015-07-31 
2015-07-31 
............. and so on... 

但是,所有我想要的是apprictaed月份的最后一个日期的数量,所以基本上如果我迭代10个月以上,我只需要10个值。 所以,我试图通过以下逻辑提前30天提高索引。

start = 10.months.ago.to_date 
end_date = Date.today 
(start..end_date).each do |date| 
    puts date.end_of_month 
    date = date.end_of_month + 1.day 
end 

但我仍正在以下的输出:

2015-07-31 
2015-07-31 
2015-07-31 
2015-07-31 
2015-07-31 
2015-07-31 
2015-07-31 
2015-07-31 
2015-07-31 
2015-07-31 
2015-07-31 
2015-07-31 
2015-07-31 
2015-07-31 
2015-07-31 
2015-07-31 
2015-07-31 
2015-07-31 
2015-07-31 
2015-07-31 
........................... and so on 

我虽然增加指标值可能会改变结果,但事实并非如此。 请有人可以提出任何解决方案吗?

编辑: 我想我找到了答案,而不是itterating某一日期范围内,我用while循环来检查end_date

这种情况是我做过什么:

start = 10.months.ago.to_date 
    end_date = Date.today 
    date_i = start 
    while(date_i < end_date) 
     puts date_i.end_of_month 
     date_i = date_i + 1.month 
    end 

输出:

2015-07-31 
2015-08-31 
2015-09-30 
2015-10-31 
2015-11-30 
2015-12-31 
2016-01-31 
2016-02-29 
2016-03-31 
2016-04-30 
+0

编写的SQL是不是一种选择?请提供您的表格和模型结构。 – Uzbekjon

+0

完成了,即使从用户投资的日期到当前的数据得到每月的最后值,也是好的。 – VoidZero

+0

建议的答案是可以的,但你应该让DB为你做这个。这样你就可以节省资源。你在用什么数据库? – Uzbekjon

回答

1

你需要做的是,你必须采取金额或投资的所有价值或任何你想要的一个月的任何。

现在添加它们。

并取平均值。

例如。我们采取当月

total_amount = UserPortfolio.where(created_at: (Date.today.beginning_of_month..Date.today.end_of_month)).pluck(:amount) 
average_amount = total_amount.inject(0, :+)/total_amount.count 

这会给你平均当月 同样你可以得到每一个月为全年。

然后把它放在一个散列或数组中,并在图中显示它们。

+0

这将只给出本月的结果 –

+0

其实我并不想要平均值,但结束日期的金额一个月。 – VoidZero

+0

然后只是使用(Date.today.end_of_month)而不是(Date.today.beginning_of_month..Date.today.end_of_month) –

1

尝试:

> months_result = user_portfolio.user_portfolio_growths.group_by{|e| e.created_at.strftime('%B')} 
> month_with_total = months_result.each{|month, data| [month, data.pluck(:appreciated_value).sum]}.to_h 
#=> {"January" => 12546.00, "February" => 56487.00} something like this 
+0

而不是折腾代码,解释代码为什么有用以及它做了什么。这个想法是以一种教育的方式来回答,而不仅仅是解决眼前的问题。 –

相关问题