将SQL转换为HQL

时间:2017-12-17 14:38:00

标签: sql hql

我的表格如下:

Id   Timestamp            name_id     testobject_id 
----------------------------------------------------
2  | 2017-12-15 18:36:46 | 1        |  1
3  | 2017-12-15 18:36:46 | 2        |  1
1  | 2016-01-01 00:00:00 | 1        |  1
4  | 2017-12-15 18:36:46 | 1        |  2
5  | 2017-12-15 18:36:46 | 2        |  2

我想要每个组的最后一条记录(name_id)。但我只想要testobject_id = 1

的结果

这个SQL工作得很好:

SELECT *
    FROM checkresult
    WHERE test_object_id = 4
    AND timestamp IN (
        SELECT MAX(timestamp)
        FROM checkresult
        WHERE test_object_id = 4    
        GROUP BY name_id
    ); 

我的HQL不起作用。

FROM checkresult as cr 
 WHERE cr.testobject = :testobject 
  AND cr.timestamp IN ( 
   MAX(cr.timestamp) 
    FROM checkresult as result WHERE result.testobject = :testobject 
     GROUP BY result.checkName)

我感谢你的每一次帮助。

期望的输出:

Id   Timestamp            name_id     testobject_id 
----------------------------------------------------
2  | 2017-12-15 18:36:46 | 1        |  1
3  | 2017-12-15 18:36:46 | 2        |  1

1 个答案:

答案 0 :(得分:1)

您已在第二个SQL语句中编写MAX(cr.timestamp)。 但它应该是

MAX(result.timestamp) 
FROM checkresult as result WHERE result.testobject = :testobject 
GROUP BY result.checkName

你的错误信息是什么?