我有三张桌子:route(number, frequency ..), operator(name, ..) and route_operator(route_number, operator_name)
。
假设我在route_operator
表
+--------------+----------------+
| route_number | operator_name |
+--------------+----------------+
| 100 | OK Travel |
| 111 | OK Travel |
| 111 | Venture Travel |
+--------------+----------------+
和route
表:
+--------+-----------+
| number | frequency |
+--------+-----------+
| 100 | 4 |
| 111 | 4 |
+--------+-----------+
现在,使用Hibernate Criteria我想返回路由的SUM'由OK Travel
操作的频率,但是,如果有更多运营商采用此路线,则频率必须除以采用此路线的运营商数量。因此,返回的结果必须是6(路线100为4 +路线111为4/2,因为它也由Venture Travel拍摄)。
我为此编写了原始SQL:
select sum(r.frequency / (select count(*) from route_operator ro2 where ro2.route_number = r.number)) from route r inner join route_operator ro on ro.operator_name = 'OK Travel' AND r.number = ro.route_number;
这是我到目前为止使用Criteria所做的事情:
Criteria criteria = session.createCriteria(Route.class, "r");
criteria.createAlias("r.operators", "o");
criteria.add(Restrictions.eq("o.name", "OK Travel")); // now I need sum and division
如何在原始SQL中创建子查询,然后在.sum()方法中使用它?
谢谢