简体   繁体   中英

Hibernate last record in group using hibernate criteria

I have a table with the following fields: ID (unique),type, value and createddate

the data types are uniqueidentifier,varchar and datetime respectively.

sample data:

ID   type  value createddate

ui1 field1 value1 2016-02-13 16:39:21.100

ui2 field1 value2 2016-02-20 18:00:00.100

ui3  field2 value3 2016-02-13 16:39:21.200

ui4  field2 value4 2016-02-20 18:00:00.200

I need to retrieve the latest value record for each type.

Hence the expected result should be like this:

ID  type value createddate

ui2  field1 value2 2016-02-20 18:00:00.100

ui4  field2 value4 2016-02-20 18:00:00.200

I hope to carry out the sql query. I need hsql code to achieve this?

Use a HQL query like this

FROM Record r 
GROUP BY r.type 
HAVING r.createdDate >= ALL (
    SELECT r2.createdDate 
    FROM Record r2 
    WHERE r2.type = r.type
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM