简体   繁体   English

如何计算HQL中的子查询?

[英]How to count subqueries in HQL?

I need to count this subquery with HQL ( using Hibernate in JPA ) 我需要用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 "

I didn't use Hibernate lib , so I have to find away to count with HQL only. 我没有使用Hibernate lib,所以我不得不找出仅可以使用HQL的数量。 Could anyone help me? 有人可以帮我吗? Many thanks in advance ! 提前谢谢了 !

maybe should ask for more details before answering your question but it seems i don't have enough points here (or reputation) to do that like others. 也许应该在回答您的问题之前先询问更多详细信息,但似乎我在这里(或声誉)没有足够的分数来像其他人那样做。 Any way, the below is my idea. 无论如何,以下是我的想法。

Assume there is XoopsSalesOrder.Id as primary key and XoopsSalesOrder.customerCode is unique. 假设有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
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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