2016-05-15 22 views
0

我有以下查詢ActiveRecord的計算列的和與集團他們

engagement_metrics = EngagementMetric.where(engagement_id: engagement_ids).order('metrics_date desc').limit(7).group_by { |p| p.metrics_date } 

導致這樣的事情

{ 
    "2016-05-13": [ 
     { 
      "id": 4, 
      "provider": "facebook", 
      "likes": -2, 
      "comments": 0, 
      "shares": 0, 
      "views": 0, 
      "reach": 0, 
      "reactions": { 
       "sad_count": "0", 
       "wow_count": "-1", 
       "haha_count": "0", 
       "like_count": "-1", 
       "love_count": "0", 
       "angry_count": "0" 
      } 
     }, 
     { 
      "id": 5, 
      "provider": "facebook", 
      "likes": 2, 
      "comments": 2, 
      "shares": 2, 
      "views": 2, 
      "reach": 0, 
      "reactions": { 
       "sad_count": "0", 
       "wow_count": "0", 
       "haha_count": "0", 
       "like_count": "0", 
       "love_count": "0", 
       "angry_count": "0" 
      } 
     } 
    ], 
    "2016-05-12": [ 
     { 
      "id": 3, 
      "provider": "facebook", 
      "likes": 1, 
      "comments": 3, 
      "shares": 0, 
      "views": 0, 
      "reach": 0, 
      "reactions": { 
       "sad_count": "1", 
       "wow_count": "0", 
       "haha_count": "0", 
       "like_count": "0", 
       "love_count": "0", 
       "angry_count": "0" 
      }, 
      "engagement_id": 1, 
      "participation_id": 1, 
      "campaign_id": 1, 
      "influencer_authorization_id": 1, 
      "influencer_id": 1, 
      "social_account_id": 1, 
      "metrics_date": "2016-05-12", 
      "status": "processed", 
      "deleted_at": null, 
      "created_at": "2016-05-14T11:36:55.995Z", 
      "updated_at": "2016-05-14T11:36:55.995Z" 
     } 
    ], 
    "2016-05-11": [ 
     { 
      "id": 2, 
      "provider": "facebook", 
      "likes": 0, 
      "comments": 16, 
      "shares": 0, 
      "views": 0, 
      "reach": 0, 
      "reactions": { 
       "sad_count": "0", 
       "wow_count": "0", 
       "haha_count": "0", 
       "like_count": "0", 
       "love_count": "0", 
       "angry_count": "0" 
      } 
     } 
    ], 
    "2016-05-10": [ 
     { 
      "id": 1, 
      "provider": "facebook", 
      "likes": 3, 
      "comments": 4, 
      "shares": 0, 
      "views": 0, 
      "reach": 0, 
      "reactions": { 
       "sad_count": "0", 
       "wow_count": "1", 
       "haha_count": "0", 
       "like_count": "1", 
       "love_count": "1", 
       "angry_count": "0" 
      } 
     } 
    ] 
} 

這是遍歷得到的數據如下圖所示的最佳方式

[ 
    { 
     "date": "24/03/16", 
     "metrics": { 
      "likes_count": "29", 
      "comments_count": "456", 
      "shares_count": "234", 
      "views_count": "65", 
      "clicks_count": "123" 
     } 
    }, 
    { 
     "date": "25/03/16", 
     "metrics": { 
      "likes_count": "345", 
      "comments_count": "234", 
      "shares_count": "876", 
      "views_count": "345", 
      "clicks_count": "45" 
     } 
    }, 
    { 
     "date": "26/03/16", 
     "metrics": { 
      "likes_count": "345", 
      "comments_count": "265", 
      "shares_count": "243", 
      "views_count": "165", 
      "clicks_count": "87" 
     } 
    }, 
    { 
     "date": "27/03/16", 
     "metrics": { 
      "likes_count": "376", 
      "comments_count": "87", 
      "shares_count": "54", 
      "views_count": "754", 
      "clicks_count": "34" 
     } 
    }, 
    { 
     "date": "28/03/16", 
     "metrics": { 
      "likes_count": "103", 
      "comments_count": "324", 
      "shares_count": "405", 
      "views_count": "87", 
      "clicks_count": "354" 
     } 
    }, 
    { 
     "date": "29/03/16", 
     "metrics": { 
      "likes_count": "23", 
      "comments_count": "65", 
      "shares_count": "234", 
      "views_count": "87", 
      "clicks_count": "34" 
     } 
    }, 
    { 
     "date": "30/03/16", 
     "metrics": { 
      "likes_count": "98", 
      "comments_count": "576", 
      "shares_count": "34", 
      "views_count": "365", 
      "clicks_count": "212" 
     } 
    } 
] 

回答

2

您應該儘量在DB中使用(使用group)instea d的紅寶石代碼(group_by)出於性能原因。我認爲,你的要求,即給定列的日常資金,可以使用自定義選擇和分組檢索:

EngagementMetric. 
    select("metrics_date as date"). 
    select("sum(likes) as likes_count"). 
    select("sum(comments) as comments_count"). 
    select("sum(shares) as shares_count"). 
    select("sum(views) as views_count"). 
    select("sum(clicks) as clicks_count"). 
    where(engagement_id: engagement_ids).   
    group("date"). 
    order("date desc"). 
    to_json 

# => [{ date: "2016-05-01", likes_count: 123, comments_count: 456, ... }, {...}] 

即這將對日常組中的所有數據進行求和並返回結果。如果您希望將度量標準總和顯示在「度量標準」子項下(可使用custom as_json method完成),則只需構建一個不同的JSON即可。

+0

哇,顯着提高了性能。你可以分享任何鏈接,我可以學習如何編寫這些高級查詢更多。 –

+0

很高興它爲你工作,對不起,我不知道很多有關SQL的教程資源,但[this](https://www.1keydata.com/sql/sqlgroupby.html)在線教程對我來說看起來不錯。 – BoraMa

+0

非常感謝 –