Average date difference in QueryDSL

I have an object with a Date object representing the last activity. I would like to query the average downtime. So in SQL something like:

SELECT AVG(NOW() - idle_date) FROM mytable WHERE ....

      

I tried to calculate the difference like this:

query.singleResult(
    Expressions.dateOperation(
        Long.class, 
        Ops.DateTimeOps.DIFF_SECONDS,
        DateExpression.currentDate(),
        myTable.idleDate
    )
);

      

But that doesn't allow me to do the average over the result, just min and max.

How can I express this average by date / time difference (in seconds)?

+3


source to share


1 answer


I think you could use an object NumberTemplate

; something along these lines should do what you need:NumberTemplate.create(Double.class, "AVG(NOW() - {0})", myTable.idleDate);



0


source







All Articles