Average date difference in QueryDSL

I have an entity with a Date object representing last activity. I would like to query the average idle time. 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 does not allow me to do an average on the result, only min and max.

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

-------------Problems Reply------------

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

Category:jpa Views:2 Time:2019-01-11
Tags: jpa querydsl

Related post

  • Annotate a queryset with the average date difference? (django) 2010-06-28

    I searched all over place for an answer to this but couldn't find anything. Perhaps this is just a stupid question or a really tricky one. Here it is: Let's say my model is this (pseudo django code): Event type = ForeignKey(EventType) name = CharFiel

  • Calculate average date difference between records in MS Access 2010-03-05

    I have a list on when items have been handed out. The table has the following structure: primary key - autonumber itemname itemid - number datehandedout - date/time I want to calculate the average length of time between when one object is given out a

  • Date Difference in Years C# 2010-11-08

    How can I calculate date difference between two dates in Years. For example: (Datetime.Now.Today() - 11/03/2007) in years. --------------Solutions------------- Heres how: TimeSpan span = DateTime.Now.Subtract(new DateTime(11, 3, 2007)); int years = (

  • Getting a Weighted Average Date Value? 2011-06-24

    If I have 50% weight on 6/3/2011 and 50% weight on 6/1/2011, the weighted average will be 6/2/2011. Now, I can't seem to figure out how I can do this with uneven weights, since it's not like you can multiply a DateTime by a double, and sum up the res

  • How do you create an average time difference between records in MySQL? 2011-09-05

    I desire to create an average time difference between device connections. I have this query now thanks to ajreal. What I need to do now is create and average time between each device record being posted. select device, count(*) as cnt, max(time) from

  • Date Difference in Javascript 2012-01-28

    I am developing a system which requires that you should be at least 18 years old to register. For doing this validation i have implemented date difference in javascript in following way, but it is not accurate, is there any javascript function or oth

  • How to find the average time difference between rows in a table? 2009-05-18

    I have a mysql database that stores some timestamps. Let's assume that all there is in the table is the ID and the timestamp. The timestamps might be duplicated. I want to find the average time difference between consecutive rows that are not duplica

  • Fast Average Square Difference Function 2009-06-10

    I'm wondering if anyone knows of a fast (i.e. O(N log(N)) ) method of calculating the average square difference function (ASDF) or average magnitude difference function (AMDF) for a periodic signal, or it is even possible. I know that one can use the

  • Date difference in English 2009-06-11

    Near duplicates How do I calculate relative time? How do I calculate someone’s age in C#? Anyone know how of a function in VB.NET or C# that will take two dates, calculate the difference and output that difference in an english string? For example if

  • Date difference in Javascript (ignoring time of day) 2009-06-24

    I'm writing an equipment rental application where clients are charged a fee for renting equipment based on the duration (in days) of the rental. So, basically, (daily fee * number of days) = total charge. For instant feedback on the client side, I'm

  • How to create a function which can return Date Difference in javascript? 2010-01-27

    i have created below function to get date difference between two dates.. pleas check it that its correct as well as how to find no of month (multiply by 30 or 31 ?) and year.. function days_between(date1, date2,datepart) { // The number of millisecon

  • How do I get a proper weighted average date in SQL 2010-02-03

    I use this query to get my average date: CONVERT(DATETIME, AVG(CONVERT(FLOAT, ChargeOffDate))) as [Average C/O Date] Which correctly give me the date '2003-08-12 14:17:22.103'. When I use this query to get the weighted average date CONVERT(DATETIME,

  • count date difference in hours using php and mysql 2010-03-16

    How can i find the date difference in hours using php and mysql both. --------------Solutions------------- Use TIMEDIFF() then take the part before the first colon to get the hours. For MySQL there are a number of DATE/TIME functions, DATEDIFF(), SUB

  • Find date difference in php? 2010-04-21

    Is there anyway to find the date difference in php? I have the input of from date 2003-10-17 and todate 2004-03-24. I need the results how many days is there within these two days. Say if 224 days, i need the output in days only. I find the solution

  • Date difference in SQL Server 2005 2010-05-24

    I have two dates of the form Start Date: 2007-03-24, End Date: 2009-06-26 Now I need to find the difference between these two in the below form: 2 years, 3 months and 2 days Can someone please … in SQL Server 2005 --------------Solutions-------------

  • Date difference in date in Oracle and Java 2010-06-23

    following problem is bothering me and I can't find any reasonable explanation and a solution. Perhaps someone could enlighten me. I have an application that calculates date difference (in days) - in a Search mask and in a Details mask. In the first m

  • Python human readable date difference 2010-06-28

    I have given date strings like these: Mon Jun 28 10:51:07 2010 Fri Jun 18 10:18:43 2010 Wed Dec 15 09:18:43 2010 What is a handy python way to calculate the difference in days? Assuming the time zone is the same. The strings were returned by linux co

  • How to calculate the date difference between 2 dates using Lotus Notes 2010-07-15

    How to calculate the date difference between 2 dates using Lotus Notes Fourmular --------------Solutions------------- If you are using lotusScript you can use either of these: Returns the difference in seconds as a double difference = notesdatetimeob

  • How do I calculate the date difference? 2010-08-13

    How to calculate the date difference in Android? --------------Solutions------------- If you can't afford 3rd party libraries like JodaTime, then your best bet is really java.util.Calendar. You can use the Calendar#add() method in a loop to calculate

  • Data difference between two database 2010-08-25

    I am working on SQL Server 2005. I have took the backup and restore the database. I just wanted to know is there any way I can check whether the data between these two database are diffent. or any script that I can run or check [ could be table by ta

  • Date difference duplicates query between last and penultimate dates 2010-11-17

    I have a MS Access database that contains, among other things, a 'name' field and a 'date' field. The names contain duplicates because events to individuals with the 'name' sometimes occured more than once. What I want to do is identify duplicate 'na

  • Display Date Difference in days in the Html.textbox 2010-12-10

    I have a two text boxes 'Start Date' and 'End Date' textboxes.I am using DatePicker control to select the date. I would like to display the date difference in 'Days' in third textbox when End Date gets selected automatically. Please let me know. Than

  • Calculate the date difference in the given format in PHP4 2011-01-22

    I need a function in php4 that will calculate the date difference in the provided date format. eg. $date1 = "2011-08-24 10:03:00"; $date2 = "2012-09-24 10:04:31"; $format1 = "Y W" ; //This format should return the difference in Year and week. $format

  • date difference function 2011-02-09

    can any body can teach me how to use the date difference function? using asp.net? please... --------------Solutions------------- Example var currentDate = DateTime.Now; var pastDate = new DateTime(2004, 06, 16); var timeSpan = currentDate - pastDate;

  • EXSLT date:difference "bug" when changing month 2011-03-02

    I'm experiencing some problems when using the default date:difference EXSLT template, provided at http://www.exslt.org/date/functions/difference/index.html. I've been able to narrow down the problem and find the source: my XSL processor. The problem

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.136 (s). 11 q(s)