如何计算HQL中的子查询?

时间:2012-11-30 13:48:57

标签: java spring hibernate jpa hql

我需要用HQL计算这个子查询(在JPA中使用Hibernate)

SELECT DISTINCT s.customerName, s.customerCode, MAX(s.orderDate) as last_order_date 
FROM XoopsSalesOrder s 
GROUP BY s.customerCode 
HAVING MAX(s.orderDate) 
BETWEEN '2012-7-1' AND '2012-10-1' AND MAX(s.orderDate) NOT BETWEEN '2012-10-1' AND CURRENT_DATE "

我没有使用Hibernate lib,因此我必须找到仅使用HQL计数。 谁能帮助我?非常感谢提前!

1 个答案:

答案 0 :(得分:0)

也许应该在回答你的问题之前询问更多细节,但似乎我没有足够的分数(或声誉)像其他人那样做。无论如何,下面是我的想法。

假设有XoopsSalesOrder.Id作为主键,而XoopsSalesOrder.customerCode是唯一的。

select s.customerName, s.customerCode, s.orderDate as last_order_date 
from XoopsSalesOrder s 
where s.Id in (
  select s2.Id from XoopsSalesOrder s2 
  where 
  s2.orderDate = max(s2.orderDate) 
  and s2.orderDate between '2012-7-1' AND '2012-10-1' 
  and s2.orderDate not between '2012-10-1' AND current_date 
  group by s2.customerCode
)