2012-11-30 79 views
2

我需要算上這與子查詢HQL(JPA中使用Hibernate)如何計算HQL中的子查詢?

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計數。 任何人都可以幫助我嗎?提前謝謝了 !

+0

您是否找到了一種最終沒有繪製完整結果集的方法? – ndtreviv

回答

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 
)