我有一个我们称之为 table: table(id: integer, pid: integer, end: datetime)
具有以下数据:
table:
id pid end
1 1 1
2 1 2
3 1 3
4 2 11
5 2 12
6 2 13
7 3 21
8 3 22
9 3 23
10 4 31
11 4 32
12 4 33
我需要做的是选择记录的 id,按 pid 分组,具有最高的结束值。我的输出(作为 ActiveRecord::Relation)应该如下:
[#<Table id: 3, pid: 1, end: 3>,
#<Table id: 6, pid: 2, end: 13>,
#<Table id: 9, pid: 3, end: 23>,
#<Table id: 12, pid: 4, end: 33>]
您能给我的任何方向都非常感谢。