【发布时间】:2016-06-21 17:37:15
【问题描述】:
我目前正在 Rails 中执行一条 SQL 语句,虽然它有效,但我开始意识到我需要一种不同的格式,并试图在 PostreSQL 中完成它。这是我的查询:
sql = "SELECT one_month_high - one_month_low as one_month,
three_month_high - three_month_low as three_month,
six_month_high - six_month_low as six_month,
twelve_month_high - twelve_month_low as twelve_month,
ytd_high - ytd_low as ytd,
saved_on
FROM daily_high_lows
ORDER BY saved_on DESC;"
返回:
#<PG::Result:0x007fdb4aea1fa0 status=PGRES_TUPLES_OK ntuples=380 nfields=6 cmd_tuples=380>
...
{"one_month"=>"544", "three_month"=>"214", "six_month"=>"9","twelve_month"=>"122",
"ytd"=>"143", "saved_on"=>"2016-06-09 00:00:00"}
{"one_month"=>"1283", "three_month"=>"475", "six_month"=>"22","twelve_month"=>"189",
"ytd"=>"517", "saved_on"=>"2016-06-08 00:00:00"}
我意识到我需要一种格式:
[
{
name: "One Month",
data: {
2016-06-09 00:00:00: 544,
2016-06-08 00:00:00: 1283
}
},
{
name: "Three Month",
data: {
2016-06-09 00:00:00: 214,
2016-06-08 00:00:00: 475
}
}, etc...
]
我一直在尝试研究如何做到这一点,但目前它有点超出我的能力,所以我可以使用一些方向。
【问题讨论】:
标签: ruby-on-rails json postgresql