postgres criteriaQuery with orderBy in sum

18 Views Asked by At

I'm trying to build this postgres query snippet with criteriaQuery:

select
    sum (attr order by attr2)

The problem is that because of floating point precision the order of the attr2 values is important in summation. Do you have an idea how to build this with criteriaBuilder?

I made it work with a custom pgDialect function:

    registerFunction("sum_order_by", new SQLFunctionTemplate( StandardBasicTypes.FLOAT, "sum(?1 order by ?2)"));
                Expression<Float> functionSumOrderByAttr2 = criteriaBuilder.function( "sum_order_by", Float.class,
                        root.get( "attr2" ).as( Float.class ),
                        criteriaBuilder.parameter( String.class, "orderBy") );

Is there an inherent way of doing this with criteriaQuery?

0

There are 0 best solutions below