问题
I have a table like this:
COLUMN TYPE
------------------------------
ID INTEGER
VALUE INTEGER
TIME TIMESTAMP
How can I write query with HQL selecting sum of value column groupped by time unit(f.e. groupped by day) and selecting this time unit as a second column.
I have tried to do it and got something like this: but there is no parsedatetime function in HQL so I have no idea how can I get the proper query now.
select sum(value),
parsedatetime(day(time) || '.' || month(time) || '.' || year(time) || ' ' || hour(time) ||':00:00', 'dd.MM.yy hh:mm:ss')
as xtime
from Table
group by time
I want this query to return object with 2 fields: int and java.sql.Date.
回答1:
You are grouping by whole the datetime value, that's why you haven't expected result, you must group it by the piece of datetime that you want, try something like below:
select sum(value),
day(time) as xtime
from Table
group by day(time)
来源:https://stackoverflow.com/questions/33710305/hql-select-sum-and-time-group-by-time-unit-hour-day-month-and