如何使用Postgresql查找组中最旧的记录?

Huu*_*uze 4 sql postgresql

考虑"book"表(group_id,title,check_out_date)中的以下数据集:

> 1 - "Moby Dick" - 2010-01-01
> 1 - "The Jungle Book" - 2011-05-05
> 1 - "Grapes of Wrath" - 1999-01-12
> 2 - "Huckleberry Finn" - 2000-01-05
> 2 - "Tom Sawyer" - 2011-06-12
Run Code Online (Sandbox Code Playgroud)

我需要编写一个查询,该查询将返回包含每个组(组1和组2)中最早的"check_out_date"值的记录.这应该相当容易 - 我只是不知道该怎么做.

Nul*_*Ref 8

我想你需要这样的东西.

 select group_id, title, check_out_date from book b1 
       where
       check_out_date = 
       (select MIN(check_out_date) 
       from book b2 where b2.group_id =  b1.group_id)
Run Code Online (Sandbox Code Playgroud)