Hi,
In MySQL, I can do this:
Code:
select count(distinct substring(timestamp, 12, 2)) from mytable
where mytable has a column named timestamp of type datetime, and I get back the count of the unique hours in my table (24 if every hour of the day has at least one record). But I cannot figure out how to do that in Hibernate (Hibernate-Version: 3.1.3):
Code:
sql = "select count(distinct substring(timestamp,12,2)) from MyTableBean";
Query query = session.createQuery(sql);
List list = query.list();
produces the error:
Code:
- line 1:32: expecting CLOSE, found '('
where column 32 is the '(' after the substring. The statement works (but doesn't do what I need it to do...) without the 'substring' function:
Code:
sql = "select count(distinct timestamp) from MyTableBean";
and actually getting the data works with the substring:
Code:
sql = "select distinct substring(timestamp,12,2) from MyTableBean";
Query query = session.createQuery(sql);
List list = query.list();
gives me the rows of data.
Not sure if it is related, but I cannot coerce those results into a class:
Code:
sql = "select new MyClass(distinct substring(timestamp,12,2)) from MyTableBean";
produces an error regarding the 'distinct' keyword.
My suspicion is that I am missing something in the syntax for both problems. The first issue is a bit of a show stopper; the second one ('new MyClass(...)') I can work around by simply getting the default List object and then instantiating and populating my MyClass instances myself.
Can anyone point out the error of my ways, or suggest an alternative way of getting the count?
Thanks,
Doug.