【问题标题】:To get Max count from Hibernate Group By从 Hibernate Group 中获取最大计数
【发布时间】:2018-01-25 09:36:25
【问题描述】:

如何使用标准和投影在休眠中实现以下代码:

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
)

【问题讨论】:

    标签: hibernate projection hibernate-criteria


    【解决方案1】:

    查询可改为: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();
    

    【讨论】:

    • criteria.setProjection(Projections.projectionList().add(Projections.property("CUSTOMER_NO")).add(Projections.rowCount()).add(Projections.sqlGroupProjection("CUSTOMER_NO", " CUSTOMER_NO 有 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[] {})));
    • select count() as count, CUSTOMER_NO from temp.t group by CUSTOMER_NO with count() = (select max(tbl.c) as count from (select count( *) as c from temp.t group by CUSTOMER_NO) as tbl);
    猜你喜欢
    • 2018-01-13
    • 2017-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多