【问题标题】:Get only one record from each day每天只获取一条记录
【发布时间】:2019-02-04 23:40:37
【问题描述】:

我有一个货币表,我想在 1 周内捕获所有具有日期值的值。我做了这个查询并设法得到结果:

Currency.where (date: (1.week.ago..Date.today.end_of_day)

此日期字段是日期时间。

我现在只想获取当天记录的最高日期的记录。也就是说,我想获取 7 条记录(一周中的每一天),这条记录是该特定日期的最后一条记录(日期列中的最大值)。

如何在活动记录中进行此查询?

我尝试使用 date_trunc 但没有成功。我的数据库是postgresql

DLL 的表:

Currency(id: integer, currency_kind: integer, value: float, date: datetime, created_at: datetime, updated_at: datetime)

class CreateCurrencies < ActiveRecord::Migration[5.2]
  def change
    create_table :currencies do |t|
      t.integer :currency_kind
      t.float :value
      t.datetime :date

      t.timestamps
    end
  end
end

以 Currency.all 为例

=> #<ActiveRecord::Relation [#<Currency id: 51, currency_kind: "eur", value: 4.18835, date: "2019-02-04 05:37:59", created_at: "2019-02-04 03:08:08", updated_at: "2019-02-04 03:08:08">, #<Currency id: 52, currency_kind: "usd", value: 3.6593, date: "2019-02-04 05:37:59", created_at: "2019-02-04 03:08:08", updated_at: "2019-02-04 03:08:08">, #<Currency id: 53, currency_kind: "aud", value: 2.65061, date: "2019-02-04 05:37:59", created_at: "2019-02-04 03:08:08", updated_at: "2019-02-04 03:08:08">, #<Currency id: 54, currency_kind: "eur", value: 4.18755, date: "2019-02-04 02:47:58", created_at: "2019-02-04 03:18:13", updated_at: "2019-02-04 03:18:13">, #<Currency id: 55, currency_kind: "usd", value: 3.6593, date: "2019-02-04 02:47:59", created_at: "2019-02-04 03:18:13", updated_at: "2019-02-04 03:18:13">, #<Currency id: 56, currency_kind: "aud", value: 2.6497, date: "2019-02-04 02:47:59", created_at: "2019-02-04 03:18:13", updated_at: "2019-02-04 03:18:13">, #<Currency id: 57, currency_kind: "eur", value: 4.19655, date: "2019-02-04 22:55:59", created_at: "2019-02-04 23:26:40", updated_at: "2019-02-04 23:26:40">, #<Currency id: 58, currency_kind: "usd", value: 3.6692, date: "2019-02-04 22:55:59", created_at: "2019-02-04 23:26:40", updated_at: "2019-02-04 23:26:40">, #<Currency id: 59, currency_kind: "aud", value: 2.6499, date: "2019-02-04 22:55:59", created_at: "2019-02-04 23:26:40", updated_at: "2019-02-04 23:26:40">, #<Currency id: 60, currency_kind: "eur", value: 4.1943, date: "2019-02-04 23:27:02", created_at: "2019-02-04 23:27:09", updated_at: "2019-02-04 23:27:09">, ...]>

【问题讨论】:

    标签: ruby-on-rails postgresql activerecord


    【解决方案1】:

    你需要这样的东西

    Currency.select("rate, MAX(DATE_FORMAT(date, '%Y-%m')) as max_date").where(date: 1.week.ago..Date.today.end_of_day).group("DATE_FORMAT(date, '%Y-%m')")
    

    【讨论】:

    • 老实说,它是给mysql的,对于PG来说可能还有另外一个日期格式化函数。
    • 我尝试:Currency.select("value, currency_kind, MAX(to_char(date, '%Y-%m')) as max_date").where(date: 1.week.ago. .Date.today.end_of_day).group("to_char(date, '%Y-%m')") 但我收到此错误:PG::GroupingError: ERROR: column "currencies.value" must appear in the GROUP BY子句或在聚合函数中使用
    • 能否为您的表和一些测试数据提供创建 DDL?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-02
    • 1970-01-01
    • 1970-01-01
    • 2017-12-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多