休眠5-如何检索每个组中的最后一条记录?

时间:2019-05-09 14:22:45

标签: mysql hibernate-5.x hibernate-5

我有一张叫Sale的桌子。我想检索每辆tab <- structure(list(Groups = c("Group1", "Group1", "Group1", "Group1", "Group1", "Group2", "Group2", "Group2", "Group3", "Group3", "Group3", "Group3"), Species = c("Sp1", "Sp1", "Sp2", "Sp3", "Sp4", "Sp2", "Sp3", "Sp4", "Sp1", "Sp3", "Sp3", "Sp3"), Value = c(1L, 4L, 78L, NA, NA, 3L, 9L, 8L, 9L, 10L, 110L, 14L)), class = "data.frame", row.names = c("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12")) tab1 <- structure(list(Groups = c("Group1", "Group1", "Group1", "Group1", "Group1", "Group2", "Group2", "Group2", "Group3", "Group3", "Group3", "Group3"), Species = c("Sp1", "Sp1", "Sp2", "Sp3", "Sp4", "Sp2", "Sp3", "Sp4", "Sp1", "Sp3", "Sp3", "Sp3"), Value = c(1L, 4L, 78L, NA, NA, 3L, 9L, 8L, 9L, 10L, 110L, 14L), sp = c(3L, 3L, NA, 3L, 3L, 2L, 2L, NA, 2L, 3L, 3L, 2L), mrca = c(3L, 3L, NA, 12L, 3L, 3L, 40L, NA, 2L, 3L, 2L, 3L)), class = "data.frame", row.names = c("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12")) 汽车的最新销售记录。

type

我有一个MySQL查询,该查询为我提供了所需的结果,如下所示:

public class Sale {

   private Long id,
   private String make,
   private String model,
   private Integer year,
   private String type, // sedan, suv, van, truck etc.
   private Double price
}

我想使用Hibernate 5方法编写此查询。我在寻找类似于以下内容的代码。

SELECT s1.*
FROM sale s1 LEFT JOIN sale s2
 ON (s1.type = s2.type AND s1.id < s2.id)
WHERE s2.id IS NULL; 

0 个答案:

没有答案