【发布时间】:2016-04-25 09:54:15
【问题描述】:
我坚持这个, 我有一个文章列表,其中包含 update_at 和 updated_by。我需要编写查询,它将为我提供所选年份所有月份每个用户更新文章的计数。
谢谢
【问题讨论】:
标签: sql ruby-on-rails postgresql activerecord
我坚持这个, 我有一个文章列表,其中包含 update_at 和 updated_by。我需要编写查询,它将为我提供所选年份所有月份每个用户更新文章的计数。
谢谢
【问题讨论】:
标签: sql ruby-on-rails postgresql activerecord
试试这个:
在这里,我以 2016 年为年份,并假设您有 current_user 可供您使用
current_user.articles.where("extract(year from updated_at)= ?", 2016).count
【讨论】:
updates per user = total updates / 3000 (因为您有 3000 个用户) 或 total_updates / 2000 (因为您有 2000 个用户进行了更新)?
你可以试试这个方法:
Article.group("updated_by, date_trunc('month', updated_at)").where("extract(year from updated_at)= ?", 2016).count
它会像[updated_by, Month_NO] => count一样返回结果
【讨论】: