简体   繁体   中英

How to count subqueries in HQL?

I need to count this subquery with HQL ( using Hibernate in JPA )

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. 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.

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
)

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