-->
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.  [ 13 posts ] 
Author Message
 Post subject: oracle: group by time
PostPosted: Tue Nov 04, 2008 12:11 pm 
Newbie

Joined: Wed Jul 25, 2007 11:39 am
Posts: 11
Hello,

Why doesn't this HQL work with Oracle ?

Code:
  <query name="query.group.by">
    <![CDATA[
      FROM example.Test as test
      GROUP BY YEAR(test.created), MONTH(test.created), DAY(test.created)
    ]]>
  </query>


Exception is: java.sql.SQLException: ORA-00979: not a GROUP BY expression

Thanks!


Last edited by bhibernate on Wed Nov 05, 2008 7:07 am, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 04, 2008 2:27 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
I doubt that this query works with any database. It is translated to something similar to

Code:
select test.* from Test test
group by YEAR(test.created), MONTH(test.created), DAY(test.created)


If you are grouping by year, month and day you can't select *. Which row of the grouped ones would be selected? I know that for example, PostgreSQL will also complain with an error message, but MySQL will select a random row from each group.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 04, 2008 3:05 pm 
Newbie

Joined: Wed Jul 25, 2007 11:39 am
Posts: 11
I tested on MySQL and it works; It selects the first entry foreach group;

Could it be that Oracle doesn't go this way ?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 04, 2008 3:38 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
In my opinion this doesn't work in MySQL either. You just get a "random" row in the result instead of an exception. See http://dev.mysql.com/doc/refman/5.0/en/ ... ields.html


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 04, 2008 4:06 pm 
Newbie

Joined: Wed Jul 25, 2007 11:39 am
Posts: 11
nordborg, thanks for your anwser !

I only want to know on which days do I have entries, so the order does not matter here.

So, how should I write the select query in order to return a <Test> list ? Is there another way than using subqueries ? I thought about somethink like:

Code:
FROM example.Test AS test
WHERE test.Id = (SELECT MAX(subtest.Id) FROM example.Test as subtest WHERE subtest.created = test.created)
GROUP BY YEAR(test.created), MONTH(test.created), DAY(test.created)


Thanks!


Last edited by bhibernate on Wed Nov 05, 2008 7:08 am, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 04, 2008 5:16 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
The simplest query would be to just select the year, month and day as an Object[].

Code:
select YEAR(test.created), MONTH(test.created), DAY(test.created)
from Test test
group by YEAR(test.created), MONTH(test.created), DAY(test.created)


If you need it to return Test objects the query you posted should work. Another option is to use the "constructor" syntax as described in http://www.hibernate.org/hib_docs/v3/re ... elect.html


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 05, 2008 7:07 am 
Newbie

Joined: Wed Jul 25, 2007 11:39 am
Posts: 11
Thanks, nordborg !

Unfortunately, another problem occured :(
Hibernate cannot (or at least I cannot figure out how to) convert properly a HQL to Oracle SQL when trying to extract the hour/minute/second value for a date field;

error is: ORA-30076 invalid extract field for extract source

Any ideea ?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 05, 2008 7:17 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
I have not worked with Oracle so I have no idea what the error message means. Maybe if you post the generated SQL.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 05, 2008 7:30 am 
Newbie

Joined: Wed Jul 25, 2007 11:39 am
Posts: 11
generated code:

...
Quote:
extract(hour from test0_.created) , extract(minute from test0_.created)

...


Hibernate does not know/check that the field is of type date or timestamp, and it generates this invalid code, which is ok only for timestamp fields;


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 06, 2008 4:12 am 
Newbie

Joined: Wed Jul 25, 2007 11:39 am
Posts: 11
Any ideea ? Is changing the field type to timestamp the only solution ?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 06, 2008 4:28 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Why are you trying to extract hour and minutes from a field that doesn't contain that information?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 06, 2008 4:52 am 
Newbie

Joined: Wed Jul 25, 2007 11:39 am
Posts: 11
as far as I know, Oracle date fields contain date/time information...


Top
 Profile  
 
 Post subject: Re:
PostPosted: Mon Oct 04, 2010 9:30 am 
Newbie

Joined: Mon Oct 04, 2010 9:10 am
Posts: 1
bhibernate wrote:
as far as I know, Oracle date fields contain date/time information...

I got the same situation. I created a question in StackOverflow. Apparently, there's no way to make it work. What I think you can do is to extend the Oracle's dialect and register the extract function to use something that works.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 13 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.