2017-08-17 47 views
0

如何使用标准和投影来实现下面的代码在休眠:从休眠集团获得最大计数通过

select CUSTOMER_NO,count(*) as max_count 
from table 
group by CUSTOMER_NO 
having count(*) in 
(
select 
max(count) 
from 
(
select count(*) as count,CUSTOMER_NO 
from table 
group by CUSTOMER_NO 
) t1 
) 

回答

2

查询可以被更改为:select CUSTOMER_NO, count(*) as count from temp.t group by CUSTOMER_NO order by count desc limit 1;

出于同样的标准是:

criteria.setProjection(Projections.projectionList().add(Projections.groupProperty("CUSTOMER_NO")).add(Projections.rowCount(), "count")); 
    criteria.addOrder(Order.desc("count")); 
    criteria.setMaxResults(1); 
    return criteria.list(); 
+0

假设有两个客户相同的计数这是不行的... – Thomas

+0

criteria.setProjection(Projections.projectionList()。添加(Projections.property( “CUSTOMER_NO”))。添加( Projects.sqlGroupProjection(“CUSTOMER_NO”,“CUSTOMER_NO having count(*)=(select max(tbl.c)as count from(select count(*)as c from temp.t group by CUSTOMER_NO)as tbl)“,new String [] {},new Type [] {}))); –

+0

select count(*)as count,CUSTOMER_NO from temp.t group by CUSTOMER_NO have count(*)=(select max(tbl.c)as count from(从temp.t group by CUSTOMER_NO选择count(*)as c)作为tbl); –