java - Hibernate Criteria division inside sum -


i have 3 tables: route(number, frequency ..), operator(name, ..) , route_operator(route_number, operator_name).

assume have following records in route_operator table

+--------------+----------------+ | route_number | operator_name  | +--------------+----------------+ | 100          | ok travel      | | 111          | ok travel      | | 111          | venture travel | +--------------+----------------+ 

and route table:

+--------+-----------+ | number | frequency | +--------+-----------+ | 100    |         4 | | 111    |         4 | +--------+-----------+ 

now, using hibernate criteria want return sum of routes' frequency operated ok travel but, if there more operators taking route, frequency has divided number of operators take route. thus, returned result must 6 (4 route 100 + 4/2 route 111 because taken venture travel well).

i have written raw sql this:

select sum(r.frequency / (select count(*) route_operator ro2 ro2.route_number = r.number)) route r inner join route_operator ro on ro.operator_name = 'ok travel' , r.number = ro.route_number; 

here's have done far using criteria:

criteria criteria = session.createcriteria(route.class, "r"); criteria.createalias("r.operators", "o"); criteria.add(restrictions.eq("o.name", "ok travel")); // need sum , division 

how can create subquery in raw sql , use in .sum() method?

thank you


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -