我有两个模特:
# Table name: work_schedules
#
# id :integer not null, primary key
# start_time :time not null
# end_time :time not null
# day_of_week :string not null
# updated_at :datetime not null
# person_id :integer
# Table name: people
#
# id :integer not null, primary key
# pesel :string not null
# first_name :string not null
# last_name :string not null
我要找最忙的员工查询。首先,我应该计算小时数(end_time- start_time)和计数day_of_week(唯一属性范围: person_id)。我不知道如何创建正确的查询。我试过的是:
@most_busy = Person.joins(:work_schedules).where(%q{end_time - start_time}).first
发布于 2016-06-08 06:28:58
适用于一周中的任何一天(即一周中所有日子的总结):WorkSchedule.group(:person_id).sum('extract(epoch from work_schedules.end_time) -extract(epoch from work_schedules.start_time)').sort_by { |_,v| -v}.first.first
这将给出在整个WorkSchedules中工作最多的人的ID。我只用PostgreSQL测试过它。
在这个查询中发生了什么:
https://stackoverflow.com/questions/37704990
复制