-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: Date Difference Calculation Conundrum
PostPosted: Wed Feb 01, 2006 10:54 am 
Newbie

Joined: Wed Feb 01, 2006 10:22 am
Posts: 6
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version: 3.0

Full stack trace of any exception that occurs:n/a

Mapping Document (partial)
Code:
... class="ProcessLog"
        <property name="timestamp" column="entryTime" type="java.util.Date"/>
        <property name="childEntry" type="ProcessLog"
          many-to-many specification...
       <property name="entryType" column="type" type="String"/>
       <property name="source" column="entrySource"/>
   ...


Name and version of the database you are using:MySQL 4.1

The generated SQL (show_sql=true): n/a

This is more a "how to" question that a specific error I am getting.
I have searched the forums, and the net, read the FAQ's etc, and am not finding an answer that seems to work.

Problem: Find the timespan between 2 dates - in any unit.

I've Tried Looking at the various HQL functions (month(timestamp), hour(timestamp), etc). these functions return that part of the timestamp, but I'm looking for the difference between 2 dates.

I came up with a query in the hql scratchpad that appears to return the number of seconds, but it doesn't work in hql, and quite frankly I don't like that I had to figure out that it was returning seconds.... this could be different between RDMS(?).

So I have an hql query that looks like (mostly):
Code:
select pl.source, avg(pl.childEntry.entryTime - pl.entryTime)
from ProcessLog pl
where pl.childEntry != null AND
         pl.entryType = 'info'
group by pl.source


From the hibernate tools, this is returning me a source and a number - some difference that is calculated I'm not sure how.

when run in the application i get an exception regarding "cannot convert value 2 from 12234.72 to TIMESTAMP", so the tool appears to be doing some translation to present the results.

This looks interesting:
Code:
select pl.source, avg( hour(pl.childEntry.entryTime - pl.entryTime))
from ProcessLog pl
where pl.childEntry != null AND
         pl.entryType = 'info'
group by pl.source


but if there are more than 24 hours difference, this fails as only the hours part of the difference is taken into the average.
I've looked at performing this cruft
Code:
    (day(pl.childEntry.entryTime - pl.entryTime)*60*24) +
    (hour(pl.childEntry.entryTime - pl.entryTime)*60) +
    (minute(pl.childEntry.entryTime - pl.entryTime))
to get to a number of minutes, but I keep thinking there has got to be a better way.

the only thing I haven't tried is to map the column with type="timestamp", but even after reading the reference docs I'm not sure what kind of impact that would have, as I am sure that at least for now the DB is returning dates of a precision that is fine for my purposes.

Is there an easier way? is there something simple I missed in the docs or HIA? is there more information/clarification I can provide?

your help is greatly appreciated.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 01, 2006 11:10 am 
Regular
Regular

Joined: Wed Jun 29, 2005 11:14 pm
Posts: 119
Location: København
Just a thought:

select pl.source, avg( second(pl.childEntry.entryTime) - second(pl.entryTime))
from ProcessLog pl
where pl.childEntry != null AND
pl.entryType = 'info'
group by pl.source


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 01, 2006 12:08 pm 
Newbie

Joined: Wed Feb 01, 2006 10:22 am
Posts: 6
you'd think that was the answer, but that gives you ONLY the seconds as far as I can tell. so the difference between 10:00:00 and 10:30:01 is only 1 second, not 30*60+1, which is what is desired... but thanks for the response.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 02, 2006 12:43 pm 
Regular
Regular

Joined: Wed Jun 29, 2005 11:14 pm
Posts: 119
Location: København
It shouldn't - note that I do 2 seconds() - you only do one...

I haven't tested it but I'm surprised. Please check you do avg( second(X) - second(Y)) and not avg(second(X-Y)) which is what you posted.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Mar 18, 2006 12:36 am 
Newbie

Joined: Fri Mar 17, 2006 3:54 pm
Posts: 6
Hi,

I am wondering if you found an answer to your question: basically return a date difference in some unit....I tried doing a select timecol1-timecol2 and that does return a value that seems to be using a logic where

1sec=1
1min=100
1 hour=10000
etc....

so i might be able to work back the actual difference in seconds, minutes....
The trouble is I have a feeling the value returned might be db specific and i don't want that. Thoughts?

Nicolas


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.