找到postgres rails中json字段的平均值

时间:2017-02-24 05:13:18

标签: postgresql ruby-on-rails-4 postgresql-9.4

id  name    overview                created_at
1   john    { 'keywords' => '10' }  2017-02-11 04:55:23 UTC
2   robert  { 'keywords' => '20' }  2017-02-12 04:55:23 UTC
3   litty   { 'keywords' => '10' }  2017-02-13 04:55:23 UTC
4   john    { 'keywords' => '20' }  2017-02-14 04:55:23 UTC
5   john    { 'keywords' => '30' }  2017-02-15 04:55:23 UTC
6   litty   { 'keywords' => '20' }  2017-02-16 04:55:23 UTC
7   robert  { 'keywords' => '40' }  2017-02-17 04:55:23 UTC
8   litty   { 'keywords' => '50' }  2017-02-18 04:55:23 UTC

通过唯一 name查找记录并拥有max(created_at)。 输出应该如下

id  name    overview                created_at
5   john    { 'keywords' => '30' }  2017-02-15 04:55:23 UTC
7   robert  { 'keywords' => '40' }  2017-02-17 04:55:23 UTC
8   litty   { 'keywords' => '50' }  2017-02-18 04:55:23 UTC

然后需要找到JSON字段值overview->keywords

的平均值

基于给定示例的最终结果应为40

0 个答案:

没有答案